JDBC编程步骤

1.load the driver

  • Class.forName()或者Class.forName().newInstance()或者new DriverName(),实例化driver对象,mysql为com.mysql.jdbc.Driver()
  • 实例化时自动向DriverManager注册,不需要式调用DriverManager.registerDriver()方法

2.Connect to the database

对于Mysql: Connection conn = DriverManager.getConnection("jdbc:mysql:\\localhost\DBname?&user=username&password=pw");

对于Oracle: Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:DBname","name","password");

 

3.Execute the SQL

Statement stmt = Conn.CreateStatement();
ResultSet rs = Statement.executeQuery("sql query sentence");
ResultSet rs = Statement.executeUpdate("sql update sentence");

 

4.Retrieve the result data

循环取得结果:
while(rs.next()) {

};

 

5.Close

后打开的先close!

rs.close();
stmt.colse(); conn.close();

 

posted @ 2017-06-20 22:34  Cynthia_chao  阅读(251)  评论(0编辑  收藏  举报