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() + "元!");

// }





}





}

最新文章

  1. MongoDB学习笔记~根据子集合里某个属性排序
  2. jquery获取关于宽度和高度的尺寸问题
  3. USB
  4. noi题库(noi.openjudge.cn) 1.7编程基础之字符串T31——T35
  5. 【Origin】晨起忆梦
  6. Java学习笔记——static关键字与静态的使用方法
  7. Android给listview的item设定高度
  8. Hibernate Criterion
  9. (转)Ubuntu中使用dpkg安装deb文件提示依赖关系问题,仍未被配置
  10. Python_day1 基础语法
  11. 12块钱搭建一个ss(包括一个免费服务器)
  12. Android 之旅开始了!先自我了解下Android与Linux之间的关系
  13. java集合框架之HashMap
  14. ArcGIS对SLD样式的支持
  15. C++ bitset 常用函数及运算符
  16. FileInputStream、FileReader、FileWriter和File
  17. [快速幂][NOIP2012]转圈游戏
  18. 一步步实现windows版ijkplayer系列文章之七——终结篇(附源码)
  19. 在ros功能包CMakeLists.txt中获取所在功能包的路径 便于添加第三方库的相对路径
  20. Tomcat学习总结(4)——基于Tomcat7、Java、WebSocket的服务器推送聊天室

热门文章

  1. 页面出现AXURE RP EXTENSION,怎么办?
  2. python3.x 学习笔记1(基础知识)
  3. Android RecyclerView和ScrollView嵌套使用
  4. net实现压缩功能
  5. Sublime Text 3破解
  6. Qihoo 360 altas 实践
  7. linux 常用文本操作相关命令
  8. Hdu 4930 斗地主
  9. Unity C# 设计模式(三)工厂方法模式
  10. 网络编程select函数