java数据库问题,跟着教材走,所以一直在用Access, 如果用自带的 jdk自带的数据库,驱动
发布网友
发布时间:2022-05-29 07:41
我来回答
共1个回答
热心网友
时间:2023-10-11 10:55
JDK自带的数据库,你指的是Derby吧,JDK6之后自带的微型数据库。默认安装之后会放在jdk下面的db包中,你可看看下面的示例代码:
try { // load the driver
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); System.out.println("Load the embedded driver");
Connection conn = null;
Properties props = new Properties();
props.put("user", "user1");
props.put("password", "user1");
//create and connect the database named helloDB
conn=DriverManager.getConnection("jdbc:derby:helloDB;create=true", props);
System.out.println("create and connect to helloDB");
conn.setAutoCommit(false);
// create a table and insert two records
Statement s = conn.createStatement();
s.execute("create table hellotable(name varchar(40), score int)");
System.out.println("Created table hellotable");
s.execute("insert into hellotable values('Ruth Cao', 86)");
s.execute("insert into hellotable values ('Flora Shi', 92)");
// list the two records
ResultSet rs=s.executeQuery("SELECT name, score FROM hellotable ORDER BY score");
System.out.println("name\t\tscore");
while(rs.next()) {
StringBuilder builder = new StringBuilder(rs.getString(1));
builder.append("\t");
builder.append(rs.getInt(2));
System.out.println(builder.toString());
}
// delete the table
s.execute("drop table hellotable");
System.out.println("Dropped table hellotable");
rs.close();
s.close();
System.out.println("Closed result set and statement");
conn.commit();
conn.close();
System.out.println("Committed transaction and closed connection");
try { // perform a clean shutdown
DriverManager.getConnection("jdbc:derby:;shutdown=true");
} catch (SQLException se) {
System.out.println("Database shut down normally");
}
}catch(Exception ex){}
System.out.println("SimpleApp finished");
追问非常感谢,请问关于
conn=DriverManager.getConnection("jdbc:derby:helloDB;create=true"
里的 括号里最后一语句 create=true 是什么意思,我以为 getConnetion就一个参数
追答create=true表示如果数据库不存在则创建