使用PreparedStatement的JDBC public static void login() throws ClassNotFoundException, SQLException{//注册驱动Class.forName("org.gjt.mm.mysql.Driver");//获取连接Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3
public static void login() throws ClassNotFoundException, SQLException{ //注册驱动 Class.forName("org.gjt.mm.mysql.Driver"); //获取连接 Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/teachmanage","dev","dev"); //编写sql String sql="select *from class_main where cid=?"; //创建预处理对象 PreparedStatement pstmt=conn.prepareStatement(sql); //设置参数(占位符) pstmt.setInt(1,2); //执行查询操作 ResultSet rs=pstmt.executeQuery(); //结果集 int i=1; if(rs.next()){ while(i<6){ System.out.print(rs.getString(i++)); } System.out.println("输完"); } if(rs!=null)rs.close(); if(pstmt!=null)pstmt.close(); if(conn!=null)conn.close(); }