注册jdbc驱动程序的三种方式
2017-05-05 09:36
453 查看
1、推荐使用
try{
Class.forName("com.mysql.jdbc.Driver");//注册驱动
String url="jdbc:mysql://localhost:3306/databasename";
Connection conn=DriverManager.getConnection(url,"username","password");//创建连接对象
Statement stmt=conn.createStatement();//创建一个SQL语句的发送命令对象
ResultSet rs=stmt.executeQuery("select * from tablename");//执行SQL,拿到查询的结果集对象
while(rs.next()){//不断指向下一条记录
System.out.println("No:"+rs.getInt(1));
System.out.println("\tName:"+rs.getString(2));
System.out.println("\tBirthday:"+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}catch(ClassNotFoundException e){
System.out.println("找不到指定的驱动程序类!");
}catch(SQLException e){
e.printStackTrace();
}
2、通过系统的属性设置
try{
System.setProperty("jdbc.driver","com.mysql.jdbc.Driver");//系统属性指定数据库驱动
String url="jdbc:mysql://localhost:3306/databasename";
Connection conn=DriverManager.getConnection(url,"username","password");//创建连接对象
Statement stmt=conn.createStatement();//创建一个SQL语句的发送命令对象
ResultSet rs=stmt.executeQuery("select * from tablename");//执行SQL,拿到查询的结果集对象
while(rs.next()){//不断指向下一条记录
System.out.println("No:"+rs.getInt(1));
System.out.println("\tName:"+rs.getString(2));
System.out.println("\tBirthday:"+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
3、看起来比较直观的一种方式,注册相应的db的jdbc驱动,3在编译时需要导入对应的lib
try{
new com.mysql.jdbc.Driver();//创建driver对象,加载数据库驱动
String url="jdbc:mysql://localhost:3306/databasename";
Connection conn=DriverManager.getConnection(url,"username","password");//创建连接对象
Statement stmt=conn.createStatement();//创建一个SQL语句的发送命令对象
ResultSet rs=stmt.executeQuery("select * from tablename");//执行SQL,拿到查询的结果集对象
while(rs.next()){//不断指向下一条记录
System.out.println("No:"+rs.getInt(1));
System.out.println("\tName:"+rs.getString(2));
System.out.println("\tBirthday:"+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
try{
Class.forName("com.mysql.jdbc.Driver");//注册驱动
String url="jdbc:mysql://localhost:3306/databasename";
Connection conn=DriverManager.getConnection(url,"username","password");//创建连接对象
Statement stmt=conn.createStatement();//创建一个SQL语句的发送命令对象
ResultSet rs=stmt.executeQuery("select * from tablename");//执行SQL,拿到查询的结果集对象
while(rs.next()){//不断指向下一条记录
System.out.println("No:"+rs.getInt(1));
System.out.println("\tName:"+rs.getString(2));
System.out.println("\tBirthday:"+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}catch(ClassNotFoundException e){
System.out.println("找不到指定的驱动程序类!");
}catch(SQLException e){
e.printStackTrace();
}
2、通过系统的属性设置
try{
System.setProperty("jdbc.driver","com.mysql.jdbc.Driver");//系统属性指定数据库驱动
String url="jdbc:mysql://localhost:3306/databasename";
Connection conn=DriverManager.getConnection(url,"username","password");//创建连接对象
Statement stmt=conn.createStatement();//创建一个SQL语句的发送命令对象
ResultSet rs=stmt.executeQuery("select * from tablename");//执行SQL,拿到查询的结果集对象
while(rs.next()){//不断指向下一条记录
System.out.println("No:"+rs.getInt(1));
System.out.println("\tName:"+rs.getString(2));
System.out.println("\tBirthday:"+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
3、看起来比较直观的一种方式,注册相应的db的jdbc驱动,3在编译时需要导入对应的lib
try{
new com.mysql.jdbc.Driver();//创建driver对象,加载数据库驱动
String url="jdbc:mysql://localhost:3306/databasename";
Connection conn=DriverManager.getConnection(url,"username","password");//创建连接对象
Statement stmt=conn.createStatement();//创建一个SQL语句的发送命令对象
ResultSet rs=stmt.executeQuery("select * from tablename");//执行SQL,拿到查询的结果集对象
while(rs.next()){//不断指向下一条记录
System.out.println("No:"+rs.getInt(1));
System.out.println("\tName:"+rs.getString(2));
System.out.println("\tBirthday:"+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
相关文章推荐
- 注册jdbc驱动程序的三种方式
- 注册jdbc驱动程序的三种方式
- 注册jdbc驱动程序的三种方式
- 注册Jdbc驱动程序的三种方式
- 注册jdbc驱动程序的三种方式
- JDBC注册驱动程序三种方式
- 注册jdbc驱动程序的三种方式
- JDBC注册驱动程序的三种方式
- 注册jdbc驱动程序的三种方式
- 注册jdbc驱动程序的三种方式
- 注册jdbc驱动程序的三种方式
- 注册jdbc驱动程序的三种方式
- JDBC注册驱动程序三种方式
- 注册jdbc驱动程序的三种方式
- 注册jdbc驱动程序的三种方式
- JDBC注册驱动程序三种方式
- 注册Jdbc驱动程序的三种方式
- 注册Jdbc驱动程序的三种方式
- JDBC注册驱动程序三种方式
- JDBC注册驱动程序三种方式