当前位置:文档之家› Java连接数据库程序实例

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));}elseSystem.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 (姓名,性别,年龄) V ALUES('吴化龙','男',20)");int count2=stmt.executeUpdate("INSERT INTO table1 (姓名,性别,年龄) V ALUES('王一飞','男',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()); //打印异常信息}}}。

相关主题