Java连接数据库程序实例

JDBC-ODBC桥连接Access实例(1

import java.sql.*;

public class JDBCDemo{

public static void main(String args[]){

JDBCDemo obj = new JDBCDemo();

obj.AccessDB();

}

Connection theConnection; //声明数据库连接对象

Statement theStatement; //声明发送到数据库执行的SQL命令的对象

ResultSet theResult; //读取的数据的结果集对象

ResultSetMetaData theMetaData; //此对象用于数据库命令执行后,返回结果信息

String theDataSource; //被访问数据库或者数据源的名称

String theUser; //数据库的用户名

String thePassword; //数据库的密码

public void AccessDB(){

openConnection(); //调用打开数据库连接的方法

execSQLCommand(“select * from Student”); //调用执行SQL语句的方法,从数据库中读取内容

closeConnection(); //调用关闭已经打开的数据库的方法

}

public void openConnection(){

theDataSource=“jdbc:odbc:TeachingAccess”; //使用TeachingAccess数据源

theUser="";

thePassword="";

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //装载JDBC-ODBC驱动程序

theConnection=DriverManager.getConnection(theDataSource,theUser,thePassword);

System.out.println("Connect:OK");

}catch (Exception e){

handleException(e);

}

}

public void execSQLCommand(String command){

try{

theStatement=theConnection.createStatement();

theResult=theStatement.executeQuery(command);

theMetaData=theResult.getMetaData ();

int columnCount=theMetaData.getColumnCount();

System.out.println("Column Count:"+columnCount);

while(theResult.next()){

for(int i =1;i<=columnCount;i++){

String colValue=theResult.getString(i);

if(colValue==null) colValue="";

System.out.print(colValue+";");

}

System.out.println();

}

}catch(Exception e){

handleException(e);

}

}

public void closeConnection(){

try{

theConnection.close ();

}catch(Exception e){

handleException(e);

}

}

public void handleException(Exception e){

System.out.println(e.getMessage ());

e.printStackTrace ();

if(e instanceof SQLException){

while((e=((SQLException)e).getNextException ())!=null){

System.out.println(e);

}

}

}

}

JDBC-ODBC桥连接SQLServer实例

import java.sql.*;

public class ConnectToSQLServer{

String dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver";

String dbUrl = “jdbc:odbc:studentSQLServer”; // studentSQLServer数据源

public Connection conn = null;

public PreparedStatement stmt = null;

public ConnectToSQLServer(){ //连接数据库

try{

Class.forName(dbDriver);

conn = DriverManager.getConnection(dbUrl);

}catch(Exception e){

e.printStackTrace();

}

}

public ResultSet executeQuery(String sql)throws Exception{//执行查询操作

try{

this.stmt = null;

this.stmt = conn.prepareStatement(sql);

return this.stmt.executeQuery();

}catch(SQLException e){

e.printStackTrace();

return null;

}

}

public int executeUpdate(String sql)throws Exception{ //执行添加、删除、修改操作

try{

this.stmt = null;

this.stmt = conn.prepareStatement(sql);

this.stmt.executeUpdate();

return 1;

}catch(SQLException e){

e.printStackTrace();

return 0;

}

}

public void closeDB(){ //关闭数据库

try{

conn.close();

stmt.close();

}catch(SQLException e){

e.printStackTrace();

}

}

public static void main(String[] arg0){

ConnectToSQLServer conn = new ConnectToSQLServer();

String sql = "select * from stud_info";

try{

ResultSet rs = null;

rs = conn.executeQuery(sql);

if(rs.next()){

System.out.println(rs.getString(1));

}

else

System.out.println("bad luck~~");

}catch(Exception e){}

}

}

JDBC-ODBC桥连接数据库(不使用数据源)

不使用数据源,直接连数据库Access 数据库

将前面实例中的数据源语句:

theDataSource=“jdbc:odbc:TeachingAccess”; //使用TeachingAccess数据源

修改为:

theDataSource="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\Teaching.mdb";

不使用数据源,直接连数据库SQLServer 数据库

将前面实例中的数据源语句:

String dbUrl = “jdbc:odbc:studentSQLServer”; // studentSQLServer数据源

修改为:

String dbUrl = "jdbc:odbc:driver={SQL Server};Server=127.0.0.1;DATABASE=student;UID=;PWD= ";

//其中Server=127.0.0.1是你的数据库服务器地址,这里为本机;DATABASE=student是数据库名字;UID=sa;PWD=sa分别为用户名和密码,如果没有密码的话,可以直接写 PWD=

直接用JDBC连接SQLServer实例

修改前面用JDBC-ODBC桥连接数据库的装载驱动程序的代码即可,如下所示:

public void openConnection(){

theDataSource=jdbc:sqlserver://localhost:1433;student";

theUser="";

thePassword="";

try{

//装载JDBC驱动程序 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); theConnection=DriverManager.getConnection(theDataSource,theUser,thePassword);

System.out.println("Connect:OK");

}catch (Exception e){

handleException(e);

}

}

使用executeUpdate执行Insert 语句

public class InsertRec{

public static void main(String args[]){

String url="jdbc:odbc:teachingAccess";

try{

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); //加载jdbc-odbc bridge 驱动程序

Connection con=DriverManager.getConnection(url); //与驱动器建立连接

Statement stmt=con.createStatement(); //创建一个Statement对象

//执行SQL语句

int count1=stmt.executeUpdate("INSERT INTO student (姓名,性别,年龄) VALUES('吴化龙','',20)");

int count2=stmt.executeUpdate("INSERT INTO table1 (姓名,性别,年龄) VALUES('王一飞','',21)");

System.out.println(“Insert successfully!”); //打印执行SQL命令的结果

System.out.println("Updatedrows is"+(count1+count2)+".");

stmt.close(); //关闭连接

con.close();

}catch(Exception ex){

System.out.println(ex.getMessage()); //打印异常信息

}

}

}

《Java连接数据库程序实例.doc》
将本文的Word文档下载,方便收藏和打印
推荐:
下载文档
热门推荐
相关推荐