时间:2021-07-01 10:21:17 帮助过:3人阅读
2, try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, userPwd); Statement st=dbConn.createStatement(); //建立SQL语句对象 String sg=select * from Student; //查询学生表 ResultSet rs=st.executeQuery(sg); /
2,
try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, userPwd); Statement st=dbConn.createStatement(); //建立SQL语句对象 String sg="select * from Student"; //查询学生表 ResultSet rs=st.executeQuery(sg); //执行SQL命令,返回的是ResultSet 结果集 while(rs.next()) //移动指针到下一条记录 { System.out.println(rs.getString("id")+"\t"+rs.getString("name")+"\t"+rs.getString("phone")); //输出查询记录的格式 } st.close(); dbConn.close(); System.out.println("Connection Successful!132"); //如果连接成功 控制台输出Connection Successful! } catch (Exception e) { e.printStackTrace(); }
2,(1)执行SQL语句时,处理查询或添加记录 用executeQuery(String);
(2)处理修改记录或 删除记录用 executeUpdate(String);
3.最后关闭的时候,先关闭Statement类对象,然后关闭连接
st.close();
dbConn.close();