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); //运行sqlint row = ps.executeUpdate();System.out.println(row);} catch (SQLException e) { // TODO Auto-generated catch blocke.printStackTrace();}finally{ if(con != null){ try { con.close();} catch (SQLException e) { // TODO Auto-generated catch blocke.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() + "元!"); // }} }