此文中的代码主要列出连接数据库的关键代码,其他访问数据库代码省略1、Oracle8/8i/9i数据库(thin模式)Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();String url="jdbc:oracle:thin:@localhost:1521:orcl";//orcl为数据库的SIDString user="test";String password="test";Connection conn= DriverManager.getConnection(url,user,password);2、DB2数据库Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();String url="jdbc:db2://localhost:5000/sample";//sample为你的数据库名String user="admin";String password="";Connection conn= DriverManager.getConnection(url,user,password);3、Sql Server7.0/2000数据库Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance ();String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb"; //mydb为数据库String user="sa";String password="";Connection conn= DriverManager.getConnection(url,user,password);4、Sybase数据库Class.forName("com.sybase.jdbc.SybDriver").newInstance();String url =" jdbc:sybase:Tds:localhost:5007/myDB";//myDB为你的数据库名Properties sysProps = System.getProperties();SysProps.put("user","userid");SysProps.put("password","user_password");Connection conn= DriverManager.getConnection(url, SysProps);5、Informix数据库Class.forName("rmix.jdbc.IfxDriver").newInstance();String url ="jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver; user=testuser;password=testpassword";//myDB为数据库名Connection conn= DriverManager.getConnection(url);6、MySQL数据库Class.forName("org.gjt.mm.mysql.Driver").newInstance();String url="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode= true&characterEncoding=8859_1"//myDB为数据库名Connection conn= DriverManager.getConnection(url);7、PostgreSQL数据库Class.forName("org.postgresql.Driver").newInstance();String url ="jdbc:postgresql://localhost/myDB"//myDB为数据库名String user="myuser";String password="mypassword";Connection conn= DriverManager.getConnection(url,user,password);8、odbc桥接Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //装载JDBC-ODBC驱动程序strCon = "jdbc:odbc:Test_DB"; //设置数据库连接字符串,test_DB9、access数据库直连用ODBC的Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;String url="jdbc:odbc:Driver={MicroSoft Access Driver(*.mdb)};DBQ="+application.getRealPath("/Data/ReportDemo.mdb"); Connection conn = DriverManager.getConnection(url,"","");Statement stmtNew=conn.createStatement() ;二、JDBC连接MySql方式下面是使用JDBC连接MySql的一个小的教程1、查找驱动程序MySQL目前提供的java驱动程序为Connection/J,可以从MySQL官方网站下载,并找到mysql-connector-java-3.0.15-ga-bin.jar文件,此驱动程序为纯java驱动程序,不需做其他配置。
2、动态指定classpath如果需要执行时动态指定classpath,就在执行时采用-cp方式。
否则将上面的.jar文件加入到classpath环境变量中。
3、加载驱动程序try{Class.forName(com.mysql.jdbc.Driver);System.out.println(Success loading Mysql Driver!);}catch(Exception e){System.out.println(Error loading Mysql Driver!);e.printStackTrace();}4、设置连接的urljdbc:mysql://localhost/databasename[?pa=va][&pa=va]三、以下列出了在使用JDBC来连接Oracle数据库时可以使用的一些技巧,这些技巧能够使我们更好地发挥系统的性能和实现更多的功能(系转载)。
1、在客户端软件开发中使用Thin驱动程序在开发Java软件方面,Oracle的数据库提供了四种类型的驱动程序,二种用于应用软件、applets、servlets等客户端软件,另外二种用于数据库中的Java 存储过程等服务器端软件。
在客户机端软件的开发中,我们可以选择OCI驱动程序或Thin驱动程序。
OCI驱动程序利用Java本地化接口(JNI),通过Oracle 客户端软件与数据库进行通讯。
Thin驱动程序是纯Java驱动程序,它直接与数据库进行通讯。
为了获得最高的性能,Oracle建议在客户端软件的开发中使用OCI驱动程序,这似乎是正确的。
但我建议使用Thin驱动程序,因为通过多次测试发现,在通常情况下,Thin驱动程序的性能都超过了OCI驱动程序。
2、关闭自动提交功能,提高系统性能在第一次建立与数据库的连接时,在缺省情况下,连接是在自动提交模式下的。
为了获得更好的性能,可以通过调用带布尔值false参数的Connection类的setAutoCommit()方法关闭自动提交功能,如下所示:conn.setAutoCommit(false);值得注意的是,一旦关闭了自动提交功能,我们就需要通过调用Connection 类的commit()和rollback()方法来人工的方式对事务进行管理。
3、在动态SQL或有时间限制的命令中使用Statement对象在执行SQL命令时,我们有二种选择:可以使用PreparedStatement对象,也可以使用Statement对象。
无论多少次地使用同一个SQL命令,PreparedStatement都只对它解析和编译一次。
当使用Statement对象时,每次执行一个SQL命令时,都会对它进行解析和编译。
这可能会使你认为,使用PreparedStatement对象比使用Statement对象的速度更快。
然而,我进行的测试表明,在客户端软件中,情况并非如此。
因此,在有时间限制的SQL操作中,除非成批地处理SQL命令,我们应当考虑使用Statement对象。
此外,使用Statement对象也使得编写动态SQL命令更加简单,因为我们可以将字符串连接在一起,建立一个有效的SQL命令。
因此,我认为,Statement 对象可以使动态SQL命令的创建和执行变得更加简单。
4、利用helper函数对动态SQL命令进行格式化在创建使用Statement对象执行的动态SQL命令时,我们需要处理一些格式化方面的问题。
例如,如果我们想创建一个将名字O'Reilly插入表中的SQL 命令,则必须使用二个相连的“''”号替换O'Reilly中的“'”号。
完成这些工作的最好的方法是创建一个完成替换操作的helper方法,然后在连接字符串心服用公式表达一个SQL命令时,使用创建的helper方法。
与此类似的是,我们可以让helper方法接受一个Date型的值,然后让它输出基于Oracle的to_date()函数的字符串表达式。