当前位置: 移动技术网 > IT编程>开发语言>Java > Java连接各种数据库的方法

Java连接各种数据库的方法

2019年07月22日  | 移动技术网IT编程  | 我要评论

本文实例讲述了java连接各种数据库的方法。分享给大家供大家参考。具体如下:

复制代码 代码如下:
//mysql:  
    string driver="com.mysql.jdbc.driver";   //驱动程序
     string url="jdbc:mysql://localhost:3306/db_name";    //连接的url,db_name为数据库名 
     string username="username";   //用户名
     string password="password";   //密码
     class.forname(driver).newinstance();
     connection con=drivermanager.getconnection(url,username,password);
//microsoft sql server 2.0驱动(3个jar的那个):
     string driver="com.microsoft.jdbc.sqlserver.sqlserverdriver";   //连接sql数据库的方法
     string url="jdbc:microsoft:sqlserver://localhost:1433;databasename=db_name";    //db_name为数据库名
     string username="username";   //用户名
     string password="password";   //密码
     class.forname(driver).newinstance();   //加载数据可驱动
     connection con=drivermanager.getconnection(url,username,password);   //
//microsoft sql server 3.0驱动(1个jar的那个): // 老紫竹完善
     string driver="com.microsoft.sqlserver.jdbc.sqlserverdriver";   //连接sql数据库的方法
     string url="jdbc:microsoft:sqlserver://localhost:1433;databasename=db_name";    //db_name为数据库名
     string username="username";   //用户名
     string password="password";   //密码
     class.forname(driver).newinstance();   //加载数据可驱动
     connection con=drivermanager.getconnection(url,username,password);   //
// sysbase:
     string driver="com.sybase.jdbc.sybdriver";   //驱动程序
     string url="jdbc:sysbase://localhost:5007/db_name";    //db_name为数据可名
     string username="username";   //用户名
     string password="password";   //密码
     class.forname(driver).newinstance();  
    connection con=drivermanager.getconnection(url,username,password);
//oracle(用thin模式):
     string driver="oracle.jdbc.driver.oracledriver";   //连接数据库的方法
     string url="jdbc:oracle:thin:@loaclhost:1521:orcl";   //orcl为数据库的sid
     string username="username";   //用户名
     string password="password";   //密码
     class.forname(driver).newinstance();   //加载数据库驱动
     connection con=drivermanager.getconnection(url,username,password);  
//postgresql:
     string driver="org.postgresql.driver";   //连接数据库的方法
     string url="jdbc:postgresql://localhost/db_name";    //db_name为数据可名
     string username="username";   //用户名
     string password="password";   //密码
     class.forname(driver).newinstance();  
    connection con=drivermanager.getconnection(url,username,password);
// db2:
     string driver="com.ibm.db2.jdbc.app.db2.driver";   //连接具有db2客户端的provider实例
     //string driver="com.ibm.db2.jdbc.net.db2.driver";    //连接不具有db2客户端的provider实例
     string url="jdbc:db2://localhost:5000/db_name";    //db_name为数据可名
     string username="username";   //用户名
     string password="password";   //密码
     class.forname(driver).newinstance();  
    connection con=drivermanager.getconnection(url,username,password);
//informix:
     string driver="com.informix.jdbc.ifxdriver";  
    string url="jdbc:informix-//sqli://localhost:1533/db_name:informixser=myserver";    //db_name为数据可名
     string username="username";   //用户名
     string password="password";   //密码
     class.forname(driver).newinstance();  
    connection con=drivermanager.getconnection(url,username,password);
// jdbc-odbc:
     string driver="sun.jdbc.odbc.jdbcodbcdriver";
     string url="jdbc:odbc:dbsource";   //dbsource为数据源名
     string username="username";   //用户名
     string password="password";   //密码
     class.forname(driver).newinstance();  
    connection con=drivermanager.getconnection(url,username,password);

希望本文所述对大家的java程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网