Connecting to a database
In order to access a database, you need to obtain a JDBC
Connectionobject. There are two basic ways to get a database connection:Using the JDBC
DriverManagerinterface, you can directly obtain a JDBCConnection, if you know:
- The name of the JDBC Driver class (
com.quadcap.jdbc.JdbcDriver)- The database URL (
jdbc:qed:database-name)- Any connection parameters (see below)
For QED, you could obtain a database connection using code similar to the following:
//获得驱动,建立连接
Class.forName("com.quadcap.jdbc.JdbcDriver"); java.sql.Connection conn = java.sql.DriverManager.getConnection("jdbc:qed:mydb");//mydb是要连接的数据库名称or, to create a new database:
//创建一个新的数据库
Class.forName("com.quadcap.jdbc.JdbcDriver"); java.util.Properties p = new java.util.Properties(); p.setProperty("create", "true"); java.sql.Connection conn = java.sql.DriverManager.getConnection("jdbc:qed:mynewdb", p);another way to create a new database:
//另外一种创建数据库的方法
Class.forName("com.quadcap.jdbc.JdbcDriver"); java.sql.Connection conn = java.sql.DriverManager.getConnection("jdbc:qed:mynewdb;create=true");
本文介绍了如何使用JDBC连接数据库的几种方式,包括通过JDBCDriverManager接口直接获取JDBCConnection对象,以及创建新数据库的具体步骤。
7991

被折叠的 条评论
为什么被折叠?



