JDBC三种方式加载数据库驱动(源码)
下面以Mysql为例:
1.
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCExample{
public static void main(String orgs[]){
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();
ResultSet rs=stmt.executeQuery("select * from tablename");
while(rs.next()){//不断指向下一条记录
System.out.println("DeptNo:"+rs.getInt(1));
System.out.println("\tDeptName:"+rs.getString(2));
System.out.println("\tLOC:"+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}catch(ClassNotFoundException e){
System.out.println("找不到指定的驱动程序类!");
}catch(SQLException e){
e.printStackTrace();
}
}
}
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCExample{
public static void main(String orgs[]){
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();
ResultSet rs=stmt.executeQuery("select * from tablename");
while(rs.next()){//不断指向下一条记录
System.out.println("DeptNo:"+rs.getInt(1));
System.out.println("\tDeptName:"+rs.getString(2));
System.out.println("\tLOC:"+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}catch(ClassNotFoundException e){
System.out.println("找不到指定的驱动程序类!");
}catch(SQLException e){
e.printStackTrace();
}
}
}
2.
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCExample2{
public static void main(String orgs[]){
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();
ResultSet rs=stmt.executeQuery("select * from tablename");
while(rs.next()){//不断指向下一条记录
System.out.println("DeptNo:"+rs.getInt(1));
System.out.println("\tDeptName:"+rs.getString(2));
System.out.println("\tLOC:"+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCExample2{
public static void main(String orgs[]){
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();
ResultSet rs=stmt.executeQuery("select * from tablename");
while(rs.next()){//不断指向下一条记录
System.out.println("DeptNo:"+rs.getInt(1));
System.out.println("\tDeptName:"+rs.getString(2));
System.out.println("\tLOC:"+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
3.
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCExample3{
public static void main(String orgs[]){
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();
ResultSet rs=stmt.executeQuery("select * from tablename");
while(rs.next()){//不断指向下一条记录
System.out.println("DeptNo:"+rs.getInt(1));
System.out.println("\tDeptName:"+rs.getString(2));
System.out.println("\tLOC:"+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCExample3{
public static void main(String orgs[]){
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();
ResultSet rs=stmt.executeQuery("select * from tablename");
while(rs.next()){//不断指向下一条记录
System.out.println("DeptNo:"+rs.getInt(1));
System.out.println("\tDeptName:"+rs.getString(2));
System.out.println("\tLOC:"+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
整理完闭
作者:Steven(Steven's Think out)
出处:http://shiyangxt.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。