1.jdbc的定义:
Java Data Base Connectivity,java数据库连接
2.jdbc的用法:
1>jdbc做3件事:
【1】与数据库建立连接
【2】发送操作数据库的语句
【3】处理结果
2>代码:
Connection con = DriverManager.getConnection("jdbc:odbc:wombat","login","password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
while (rs.next()) {
int x = rs.getInt("a");
String s = rs.getString("b");
float f = rs.getFloat("c");
}
3.设置表:
创建表 创建对象 执行SQL语句 输入数据 取得数据
4.建立JDBC的步骤:
1.Class.forName("acme.db.Driver")
2.Connection con = DriverManager.getConnection(url,"sunny","")
3.Statement stmt = con.createStatement()
4.ResultSet rs = stmt.executeQuery("SELECT a,b,c FROM Table2")
5.Statement接口提供了三种执行SQL语句的方法:executeQuery、executeUpdate和execute
6.关闭对象
刘宝宝专版
【3】处理结果
2>代码:
Connection con = DriverManager.getConnection("jdbc:odbc:wombat","login","password"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1"); while (rs.next()) { int x = rs.getInt("a"); String s = rs.getString("b"); float f = rs.getFloat("c"); } |