程序员使用Java程序连接各种数据库的方法介绍

程序员 责任编辑:q459565833 2015-10-31

添加老师微信

备考咨询

加我微信

摘要:Oracle8/8i/9i数据库(thin模式) Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); Stringurl="jdbc:oracle:thin:@localhost:1521:orcl";//orcl为数据库的SID Stringuser="test"; Stringpassw

    1、Oracle8/8i/9i数据库(thin模式)

    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

    Stringurl="jdbc:oracle:thin:@localhost:1521:orcl";//orcl为数据库的SID

    Stringuser="test";

    Stringpassword="test";

    Connectionconn=DriverManager.getConnection(url,user,password);

    2、DB2数据库

    Class.forName("com.ibm.db2.jdbc.app.DB2Driver").newInstance();

    Stringurl="jdbc:db2://localhost:5000/sample";//sample为你的数据库名

    Stringuser="admin";

    Stringpassword="";

    Connectionconn=DriverManager.getConnection(url,user,password);

    3、SQLServer7.0/2000数据库

    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();

    Stringurl="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";

    //mydb为数据库

    Stringuser="sa";

    Stringpassword="";

    Connectionconn=DriverManager.getConnection(url,user,password);

    4、Sybase数据库

    Class.forName("com.sybase.jdbc.SybDriver").newInstance();

    Stringurl="jdbc:sybase:Tds:localhost:5007/myDB";//myDB为你的数据库名

    PropertiessysProps=System.getProperties();

    SysProps.put("user","userid");

    SysProps.put("password","user_password");

    Connectionconn=DriverManager.getConnection(url,SysProps);

    5、Informix数据库

    Class.forName("com.informix.jdbc.IfxDriver").newInstance();

    Stringurl="jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;

    user=testuser;password=testpassword";//myDB为数据库名

    Connectionconn=DriverManager.getConnection(url);

    6、MySQL数据库

    Class.forName("org.gjt.mm.mysql.Driver").newInstance();

    //或者Class.forName("com.mysql.jdbc.Driver");

    Stringurl="jdbc:mysql://localhost/myDB?

    user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"

    //myDB为数据库名

    Connectionconn=DriverManager.getConnection(url);

    7、PostgreSQL数据库

    Class.forName("org.postgresql.Driver").newInstance();

    Stringurl="jdbc:postgresql://localhost/myDB"//myDB为数据库名

    Stringuser="myuser";

    Stringpassword="mypassword";

    Connectionconn=DriverManager.getConnection(url,user,password);

    8、access数据库直连用ODBC的

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    Stringurl="jdbc:odbc:Driver={MicroSoftAccessDriver

    (*.mdb)};DBQ="+application.getRealPath("/Data/ReportDemo.mdb");

    Connectionconn=DriverManager.getConnection(url,"","");

    StatementstmtNew=conn.createStatement();



相关推荐:

程序员关于java 泛型的基本使用

程序员通过Hibernate_tool生成代码和映射文件

程序员使用Java程序连接各种数据库的方法介绍



更多资料
更多课程
更多真题
温馨提示:因考试政策、内容不断变化与调整,本网站提供的以上信息仅供参考,如有异议,请考生以权威部门公布的内容为准!

软考备考资料免费领取

去领取

!
咨询在线老师!