- //MicrosoftAccess(ODBC)
- packagecom.jdm.Access;
- importjava.sql.Connection;
- importjava.sql.DriverManager;
- importjava.sql.ResultSet;
- importjava.sql.SQLException;
- importjava.sql.Statement;
- importcom.jdm.dmdatabase.DMsource;
- publicclassAccess{
- privateConnectioncon;
- privateStringdriverclass;
- privateStatementstatement;
- privateStringurl;
- privateResultSetresult;
- privateStringusername;
- privateStringpassword;
- publicAccess(){
- init();
- try{
- Class.forName(driverclass);
- con=DriverManager.getConnection(url,username,password);
- if(con!=null){
- statement=con.createStatement();
- inta=statement.executeUpdate("insertintouser(username,password)values('mike','tome')");
- result=statement.executeQuery("select*fromuser");
- while(result.next())
- System.out.println("user:"+result.getString(1)
- +"password:"+result.getString(2));
- System.out.print("/n");
- }
- result.close();
- statement.close();
- con.close();
- }catch(ClassNotFoundExceptione){
- e.printStackTrace();
- }catch(SQLExceptione){
- e.printStackTrace();
- }
- }
- publicvoidinit(){
- driverclass="sun.jdbc.odbc.JdbcOdbcDriver";
- url="jdbc:odbc:driver={MicrosoftAccessDriver(*.mdb)};DBQ=E://java//test.mdb";
- username="";
- password="";
- }
- publicstaticvoidmain(Stringargs[]){
- newAccess();
- }
- }
- ///
- /MicrosoftSqlServer2000(需要装sp3补丁,并开放端口)
- //
- packagecom.jdm.sqlserver;
- importjava.sql.CallableStatement;
- importjava.sql.Connection;
- importjava.sql.DriverManager;
- importjava.sql.ResultSet;
- importjava.sql.SQLException;
- importjava.sql.Statement;
- publicclassSqlLink{
- privateConnectioncon;
- privateStatementstm;
- privateResultSetresultset;
- privateStringdriverclass=null;
- privateStringurl=null;
- privateStringusername=null;
- privateStringpassword=null;
- publicSqlLink(){
- driverclass="com.microsoft.jdbc.sqlserver.SQLServerDriver";
- url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=cjgl";
- username="sa";
- password="123456";
- init();
- if(con!=null){
- try{
- con.close();
- if(con.isClosed())
- System.out.println("连接成功"+"数据源已释放");
- else
- System.out.println("连接成功"+"数据源未释放");
- }catch(SQLExceptione1){
- e1.printStackTrace();
- }
- }else{
- System.out.println("连接失败");
- }
- }
- publicvoidinit(){
- try{
- Class.forName(driverclass);
- con=DriverManager.getConnection(url,username,password);
- stm=con.createStatement();
- /*CallableStatementcall=con.prepareCall("{callADDXS(?,?,?,?,?,?,?)}");
- call.setString(1,"001118");
- call.setString(2,"王珊珊");
- call.setString(3,"女");
- call.setString(4,"通信工程");
- call.setString(5,"1988-03-21");
- call.setInt(6,40);
- call.setString(7,"良");
- call.execute();*/
- resultset=stm.executeQuery("select*fromXSwherexb='女'");
- while(resultset.next()){
- System.out
- .println(resultset.getString(1)+""
- +resultset.getString(2)+""
- +resultset.getString(3)+""
- +resultset.getString(4)+""
- +resultset.getString(5)+""
- +resultset.getString(6));
- }
- }catch(ClassNotFoundExceptione){
- e.printStackTrace();
- System.out.println("驱动类没有找到");
- }catch(SQLExceptione){
- e.printStackTrace();
- }
- }
- publicstaticvoidmain(String[]args){
- newSqlLink();
- }
- }
- ///
- //sunMySql
- packagecom.jdm.mysql;
- importjava.sql.Connection;
- importjava.sql.DriverManager;
- importjava.sql.ResultSet;
- importjava.sql.SQLException;
- importjava.sql.Statement;
- publicclassMySql{
- privateStringdriverclass;
- privateStringurl;
- privateStringusername;
- privateStringpassword;
- privateConnectioncon;
- privateStatementstm;
- privateResultSetresult;
- publicMySql(){
- init();
- try{
- Class.forName(driverclass);
- con=DriverManager.getConnection(url,username,password);
- stm=con.createStatement();
- result=stm.executeQuery("SELECT*FROMuserinfo");
- while(result.next()){
- System.out.println("用户名:"+result.getString(1)+"密码:"
- +result.getString(2)+"角色:"+result.getString(3)+"日期:"
- +result.getString(4)+"性别:"+result.getString(4));
- }
- con.close();
- }catch(ClassNotFoundExceptione){
- e.printStackTrace();
- }catch(SQLExceptione){
- e.printStackTrace();
- }
- }
- publicvoidinit(){
- driverclass="com.mysql.jdbc.Driver";
- url="jdbc:mysql://localhost:3306/test";
- username="root";
- password="123456";
- }
- publicstaticvoidmain(String[]args){
- newMySql();
- }
- }
- ///
- //华工达梦数据库DM
- packagecom.jdm.dmdatabase;
- importjava.sql.Connection;
- importjava.sql.DriverManager;
- importjava.sql.ResultSet;
- importjava.sql.SQLException;
- importjava.sql.Statement;
- publicclassDMsource{
- privateConnectioncon;
- privateStringdriverclass;
- privateStatementstatement;
- privateStringurl;
- privateResultSetresult;
- privateStringusername;
- privateStringpassword;
- publicDMsource(){
- init();
- try{
- Class.forName(driverclass);
- con=DriverManager.getConnection(url,username,password);
- if(con!=null){
- System.out.println("连接成功"+con.toString());
- con.close();
- }
- }catch(ClassNotFoundExceptione){
- e.printStackTrace();
- }catch(SQLExceptione){
- e.printStackTrace();
- }
- }
- publicvoidinit(){
- driverclass="dm.jdbc.driver.DmDriver";
- url="jdbc:dm://localhost:12345/mdj";
- username="mdj";
- password="123456";
- }
- publicstaticvoidmain(Stringargs[]){
- newDMsource();
- }
- }
- ///
- //ORACLEDATABASE
- //
- packagecom.jdm.Oracle;
- importjava.sql.Connection;
- importjava.sql.DriverManager;
- importjava.sql.ResultSet;
- importjava.sql.SQLException;
- importjava.sql.Statement;
- importcom.jdm.mysql.MySql;
- publicclassOracle{
- privateStringdriverclass;
- privateStringurl;
- privateStringusername;
- privateStringpassword;
- privateConnectioncon;
- privateStatementstm;
- privateResultSetresult;
- publicOracle(){
- init();
- try{
- Class.forName(driverclass);
- con=DriverManager.getConnection(url,username,password);
- stm=con.createStatement();
- result=stm.executeQuery("SELECT*FROMHELP");
- while(result.next()){
- System.out.println(result.getString(1)+""+result.getString(2)+""+result.getString(3));
- }
- if(con!=null)System.out.println("连接成功");
- con.close();
- }catch(ClassNotFoundExceptione){
- e.printStackTrace();
- }catch(SQLExceptione){
- e.printStackTrace();
- }
- }
- publicvoidinit(){
- driverclass="oracle.jdbc.driver.OracleDriver";
- url="jdbc:oracle:thin:@localhost:1521:XE";
- //url="jdbc:oracle:thin:@computername:host(1521):databasename"
- username="SYSTEM";
- password="123456";
- }
- publicstaticvoidmain(String[]args){
- newOracle();
- }
- }
- //推荐国产数据库系统 华工达梦 人大金仓 神舟oscr
java连接常用数据库
最新推荐文章于 2024-07-07 20:57:27 发布