做个项目使用到嵌入式的数据库
在设置主贱的时候 使用自动增量的时候遇到了问题
我使用sqlserver 里的 identity(1,1) 不行
使用oracle的auto-increment 不行
请问怎么样才可以了??
在设置主贱的时候 使用自动增量的时候遇到了问题
我使用sqlserver 里的 identity(1,1) 不行
使用oracle的auto-increment 不行
请问怎么样才可以了??
调试欢乐多
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javadb/CREATE table APP.ADDRESS (
ID INTEGER NOT NULL
PRIMARY KEY GENERATED ALWAYS AS IDENTITY
(START WITH 1, INCREMENT BY 1),
LASTNAME VARCHAR(30),
FIRSTNAME VARCHAR(30),
MIDDLENAME VARCHAR(30),
PHONE VARCHAR(20),
EMAIL VARCHAR(30),
ADDRESS1 VARCHAR(30),
ADDRESS2 VARCHAR(30),
CITY VARCHAR(30),
STATE VARCHAR(30),
POSTALCODE VARCHAR(20),
COUNTRY VARCHAR(30) )
我刚好有段代码
其中bog.sql 为放在当前目录的一个sql脚本
CREATE TABLE bog(
id int NOT NULL
PRIMARY KEY GENERATED ALWAYS AS IDENTITY
(START WITH 1, INCREMENT BY 1),
title varchar(200) NOT NULL ,
content varchar(2048) NOT NULL ,
date varchar (20) NOT NULL ,
private int NOT NULL ,
media varchar (20) NOT NULL ,
type varchar (20) NOT NULL
)
完整测试语句如下
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import java.util.Properties;
import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.sql.PreparedStatement;
import java.text.DateFormat;public class Complete {
public String framework = "embedded";
public String driver = "org.apache.derby.jdbc.EmbeddedDriver";
public String protocol = "jdbc:derby:";
protected static Properties props;
public Complete() {
try {
Class.forName(driver).newInstance();
props = new Properties();
props.put("user", "jesse");
props.put("password", "jesse");
} catch (Exception ee) {
ee.printStackTrace(System.err);
} }
public void createTable() throws SQLException {
Connection conn = DriverManager.getConnection(protocol +
"joybog", props);
try { Statement statement = conn.createStatement();
File file = new File("bog.sql");
StringBuffer sqlSB = new StringBuffer(); try {
FileInputStream fileInputStream = new FileInputStream(file);
int b;
while ((b = fileInputStream.read()) != -1) {
sqlSB.append((char) b);
}
} catch (IOException ee) {
}
System.out.println(sqlSB.toString());
// statement.execute("DROP table bog");
statement.execute(sqlSB.toString());
} catch (Exception ee) {
ee.printStackTrace(System.err);
}finally{
conn.close();
}
}
public void getContent() throws Exception {
Connection conn = DriverManager.getConnection(protocol +
"joybog", props);
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("select * from bog");
while (rs.next()) {
System.out.print(rs.getInt("id") + " " + rs.getString("title")
+ " " + rs.getString("content") + " " + " " +
rs.getString("date")
+ rs.getInt("private"));
System.out.println("");
}
conn.close();
}
public void addArcticle(String title, String content, boolean p,
String media, String type) throws SQLException {
Connection conn = DriverManager.getConnection(protocol +
"joybog", props);
String sql = "insert into bog(title,content,date,private,media,type) values(?,?,?,?,?,?)";
int pi;
// int id = 1;
try { if (p == true) {
pi = 1;
} else {
pi = 0;
}
java.util.Date d = new java.util.Date();
DateFormat df = DateFormat.getDateInstance();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, title);
ps.setString(2, content);
ps.setString(3, df.format(d).toString());
ps.setInt(4, pi);
ps.setString(5, media);
ps.setString(6, type);
ps.execute(); } catch (Exception ee) {
ee.printStackTrace(System.err);
} finally {
conn.close();
}
} public static void main(String[] args)throws Exception{
Complete cm=new Complete();
// cm.createTable();
cm.addArcticle("jesse's first bog title","the content",false,"alizee.avi","nothing");
cm.getContent();
}
}
Connection conn = DriverManager.getConnection(protocol +
"joybog", props);改为
Connection conn = DriverManager.getConnection(protocol +
"joybog:create=true", props);