package JDBC; import java.sql.*; import java.util.ResourceBundle; public class JDBC_prestatement { public static void main(String[] args){ Connection conn = null; PreparedStatement ps = null; //同步替换变量名shift+F6 ResultSet rs = null; //资源绑定器 ResourceBundle bundle = ResourceBundle.getBundle("resourses/db"); String driver= bundle.getString("driver"); String url = bundle.getString("url"); String user = bundle.getString("user"); String password = bundle.getString("password"); try{ //1.注册驱动的第二种方式:类加载注册 // Class.forName("com.mysql.jdbc.Driver");//可以写到配置文件当中 Class.forName(driver); //2.获取连接 // conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/trade_for_book","root","002415"); conn = DriverManager.getConnection(url,user,password); //3.获取数据库 *** 作对象 // 实现插入 // String sql = "insert into t_user(login_name,login_pwd,real_name) values (?,?,?)"; // ps = conn.prepareStatement(sql); // ps.setString(1,"admin_01"); // ps.setString(2,"002415"); // ps.setString(3,"xx03"); // 实现更新 // String sql = "update t_user set login_name = ? where login_name = ?"; // ps = conn.prepareStatement(sql); // ps.setString(1,"fuuuuuk"); // ps.setString(2,"admin_01"); // 实现删除 String sql = "delete from t_user where login_name = ?"; ps = conn.prepareStatement(sql); ps.setString(1,"fuuuuuk"); //4.执行sql语句 int count = ps.executeUpdate(); System.out.println(count); }catch(Exception e){ e.printStackTrace(); }finally { if (rs != null){ try{ rs.close(); }catch(SQLException e){ e.printStackTrace(); } } if (ps != null){ try{ ps.close(); }catch(SQLException e){ e.printStackTrace(); } } if (conn != null){ try{ conn.close(); }catch(SQLException e){ e.printStackTrace(); } } } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)