package com.lovo.day18_jdbc1;





import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.SQLException;





public class TestMain {





/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub



//获取连接--通用代码

Connection con = null;

try {

Class.forName("com.mysql.jdbc.Driver");         //载入驱动类

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名",         

"账号", "password");


} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}



//新增记录

//构造sql语句

String sql = "insert into t_user(f_username,f_password,f_account) values(?,?,?

)";

System.out.println(sql);

try {

//获取预编译语句对象

PreparedStatement ps = con.prepareStatement(sql);

ps.setString(1, "zhang6");

ps.setString(2, "123456");

ps.setDouble(3, 16000);


//运行sql

int row = ps.executeUpdate();

System.out.println(row);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

if(con != null){

try {

con.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}



//查询语句---查询单条记录

// UserInfo theUser = null;

// String inputName = JOptionPane.showInputDialog("请输入username:");

// String inputPwd = JOptionPane.showInputDialog("请输入password:");

// String sql = "select * from t_user where f_username = '"

// + inputName + "' and f_password = '" + inputPwd + "'";

// System.out.println(sql);

// try {

// Statement stat = con.createStatement();

// ResultSet rs = stat.executeQuery(sql);

// while(rs.next()){

// theUser = new UserInfo();

// theUser.setId(rs.getInt(1));

// theUser.setUsername(rs.getString(2));

// theUser.setPassword(rs.getString(3));

// theUser.setAccount(rs.getDouble(4));

// }

//

// } catch (SQLException e) {

// // TODO Auto-generated catch block

// e.printStackTrace();

// } finally{

// if(con != null){

// try {

// con.close();

// } catch (SQLException e) {

// // TODO Auto-generated catch block

// e.printStackTrace();

// }

// }

// }

//  //做一个推断来看成功是否

// if(theUser == null){

// JOptionPane.showMessageDialog(null, "登录失败!");

// }else{

// JOptionPane.showMessageDialog(null, "登录成功!

您的剩余金额是:" + theUser.getAccount() + "元!

");

// }



//查询多条记录

// ArrayList<UserInfo> allUsers = new ArrayList<UserInfo>();            //用一个集合来装

// String sql = "select * from t_user where f_account > 12000";

// try {

// Statement stat = con.createStatement();

// ResultSet rs = stat.executeQuery(sql);

// while(rs.next()){

// UserInfo theUser = new UserInfo();

// theUser.setId(rs.getInt(1));

// theUser.setUsername(rs.getString(2));

// theUser.setPassword(rs.getString(3));

// theUser.setAccount(rs.getDouble(4));

//

// allUsers.add(theUser);

// }

// } catch (SQLException e) {

// // TODO Auto-generated catch block

// e.printStackTrace();

// } finally{

// if(con != null){

// try {

// con.close();

// } catch (SQLException e) {

// // TODO Auto-generated catch block

// e.printStackTrace();

// }

// }

// }





//预编译语句

// UserInfo theUser = null;

// String inputName = JOptionPane.showInputDialog("请输入username:");

// String inputPwd = JOptionPane.showInputDialog("请输入password:");

// String sql = "select * from t_user where f_username = ?

and f_password = ?

";

// System.out.println(sql);

// try {

// PreparedStatement ps = con.prepareStatement(sql);

// ps.setString(1, inputName);

// ps.setString(2, inputPwd);

//

// ResultSet rs = ps.executeQuery();

// while(rs.next()){

// theUser = new UserInfo();

// theUser.setId(rs.getInt(1));

// theUser.setUsername(rs.getString(2));

// theUser.setPassword(rs.getString(3));

// theUser.setAccount(rs.getDouble(4));

// }

//

// } catch (SQLException e) {

// // TODO Auto-generated catch block

// e.printStackTrace();

// } finally{

// if(con != null){

// try {

// con.close();

// } catch (SQLException e) {

// // TODO Auto-generated catch block

// e.printStackTrace();

// }

// }

// }

//

// if(theUser == null){

// JOptionPane.showMessageDialog(null, "登录失败!");

// }else{

// JOptionPane.showMessageDialog(null, "登录成功!您的剩余金额是:" + theUser.getAccount() + "元!");

// }





}





}

mysql_jdbc的更多相关文章

  1. mysql_jdbc连接说明

    mysql JDBC Driver 常用的有两个,一个是gjt(Giant Java Tree)组织提供的mysql驱动,其JDBC Driver名称(JAVA类名)为:org.gjt.mm.mysq ...

  2. spring web.xml 难点配置总结

    web.xml web.xml是所有web项目的根源,没有它,任何web项目都启动不了,所以有必要了解相关的配置. ContextLoderListener,ContextLoaderServlet, ...

  3. java连接mysql :No Suitable Driver Found For Jdbc 解决方法

    今天出现编码出现了No suitable driver found for jdbc,又是找遍了网上的资料,基本上都说是以下个问题:    一是:连接URL格式出现了问题(Connection con ...

  4. No Suitable Driver Found For Jdbc_我的解决方法

    转载自:http://www.blogjava.net/w2gavin/articles/217864.html      今天出现编码出现了No suitable driver found for ...

  5. no suitable driver found for jdbc:mysql//localhost:3306/..

      出现这样的情况,一般有四种原因(网上查的): 一:连接URL格式出现了问题(Connection conn=DriverManager.getConnection("jdbc:mysql ...

  6. mybatis-配置文件mybatis-config.xml

    在mybatis-config.xml中有初始的配置: <!-- 对事务的管理和连接池的配置 --> <environments default="development& ...

  7. MyBatis(一):配置并使用

    MyBatis具体是什么东东,这些在后边在研究吧,本文目的是为了记录如何使用MyBatis. 首先,需要下载MyBatis开发所需要文件. 通过github上可以找到MyBatis代码:https:/ ...

  8. servlet 会话管理

    一.URL 重写 URL 重写是一种会话跟踪技术,它将一个或多个token添加到URL的查询字符串中,每个token通常为 key=value形式,如下: url?key-1=value-1& ...

  9. spring web.xml 难点配置总结【转】

    web.xml web.xml是所有web项目的根源,没有它,任何web项目都启动不了,所以有必要了解相关的配置. ContextLoderListener,ContextLoaderServlet, ...

随机推荐

  1. bzoj2438: [中山市选2011]杀人游戏(强联通+特判)

    2438: [中山市选2011]杀人游戏 题目:传送门 简要题意: 给出n个点,m条有向边,进行最少的访问并且可以便利(n-1)个点,求这个方案成功的概率 题解: 一道非常好的题目! 题目要知道最大的 ...

  2. m_Orchestrate learning system---二十三、如何搜索概念图插件

    m_Orchestrate learning system---二十三.如何搜索概念图插件 一.总结 一句话总结:要在百度你们搜索前端组件,前端组件  概念图工具,js概念图工具等等这些 用的话用go ...

  3. Webfont 的兼容性问题[持续更新]

    低版安卓手机的 webview 显示不了,另外黑莓手机显示出来是这样: 生成工具: 离线字体生成工具:webfont 在线字体生成平台:icomoon.io, iconfont.cn均有问题 其他一些 ...

  4. P1452 Beauty Contes

    题目背景 此处省略1W字^ ^ 题目描述 贝茜在牛的选美比赛中赢得了冠军”牛世界小姐”.因此,贝西会参观N(2 < = N < = 50000)个农场来传播善意.世界将被表示成一个二维平面 ...

  5. ResNet(深度残差网络)

    注:平原改为简单堆叠网络 一般x是恒等映射,当x与fx尺寸不同的时候,w作用就是将x变成和fx尺寸相同. 过程: 先用w将x进行恒等映射.扩维映射或者降维映射d得到wx.(没有参数,不需要优化器训练) ...

  6. 【翻译自mos文章】私有网络所用的协议 与 Oracle RAC

    说的太经典了,不敢翻译.直接上原文. 来源于: Network Protocols and Real Application Clusters (文档 ID 278132.1) PURPOSE --- ...

  7. Java 反射经常用法演示样例

    <pre name="code" class="java">import java.lang.reflect.Constructor; import ...

  8. 网络project1101班2014-2015学年《网络软件开发实训》期末考试

    注意事项: *考试时间:2014年11月20日 第5.6节. *在计算机D盘,新建目录.并命名"学号+姓名".如:(称为考生目录.下同).考试中全部文件必须保存在此目录下. *启动 ...

  9. ubuntu12.04更新软件源时出现校验和不符

    在运行update命令之后.出现系统校验和不符.网上找了一些方法,最后在大神的帮助下最终攻克了! ! 1.更改 /etc/apt/apt.conf.d/00aptitude 文件,在最后一行增加: A ...

  10. BZOJ2020: [Usaco2010 Jan]Buying Feed II

    [传送门:BZOJ2020] 简要题意: 约翰开车回家,遇到了双十一节,那么就顺路买点饲料吧.回家的路程一共有E 公里,这一路上会经过N 家商店,第i 家店里有Fi 吨饲料,售价为每吨Ci 元.约翰打 ...