Home  >  Article  >  Database  >  利用jdbc连接 对数据库的基本操作

利用jdbc连接 对数据库的基本操作

WBOY
WBOYOriginal
2016-06-07 15:41:301183browse

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();


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn