标签:nec int a10 math 注册 void 通过 sql oid
jar包导入
包: java.sql
javax.sql
1.加载驱动
//DriverManager.registerDriver(new Driver());
//通过源码发现Driver 类有一个静态代码块,自己创建一个对象添加进registerDriver(),我们自己又创建一个对象,造成两次注册
//com.mysql.jdbc.Driver
Class.forName("com.mysql.jdbc.Driver");
//获取连接对象
Connection coon = DriverManager.getConnection("jdbc:mysql://localhost:3306/day01","root","root");
//获取statement对象
Statement st = coon.createStatement();
//执行查询
String sql = "select * from emp";
ResultSet rs = st.executeQuery(sql);
//获取结果集中的数据
while(res.next()) {
int id = res.getInt(1);
String name = res.getString(2);
String age = res.getString(3);
int math = res.getInt(4);
int english = res.getInt(5);
System.out.println(id+" "+name +" "+age+" "+math+" "+english);
}
//释放资源
rs.close();
st.close();
coon.close();
1 package day28_test; 2 3 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 import java.sql.Statement; 9 10 import com.mysql.jdbc.Driver; 11 12 public class JDBC_Demo { 13 public static void main(String[] args) throws Exception { 14 //加载驱动 15 DriverManager.registerDriver(new Driver()); 16 //获取连接对象 17 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/day01", "root", "root"); 18 //获取Statement 19 Statement st = conn.createStatement(); 20 //执行查询 21 String sql = "select * from stu"; 22 ResultSet res = st.executeQuery(sql); 23 //获取结果集中的数据 24 while(res.next()) { 25 int id = res.getInt(1); 26 String name = res.getString(2); 27 String age = res.getString(3); 28 int math = res.getInt(4); 29 int english = res.getInt(5); 30 System.out.println(id+" "+name +" "+age+" "+math+" "+english); 31 32 } 33 //释放资源 34 res.close(); 35 st.close(); 36 conn.close(); 37 } 38 }
标签:nec int a10 math 注册 void 通过 sql oid
原文地址:https://www.cnblogs.com/star521/p/9008302.html