javaDB求解!
[code=Java][/code]
import java.sql.*;
public class myDB {
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
System.out.println("Load the embedded driver");
Connection conn=DriverManager.getConnection("jdbc:derby:myDB;create=true","ying","password");
System.out.println("create and connect to myDB");
Statement s=conn.createStatement();
System.out.println("Created table myDB");
s.execute("create table employee(no varchar(4),name varchar(8),sex varchar(2),salary float)");
s.execute("insert into employee values('1001','张强','男',675.20)");//SQL语句,字符串用''单引号
s.execute("insert into employee values('1004','李香','女',842.00)");//SQL语句,字符串用''单引号
s.execute("insert into employee values('1007','王大山','男',765.20)");//SQL语句,字符串用''单引号
s.execute("insert into employee values('1010','赵玉花','女',690.20)");//SQL语句,字符串用''单引号
//返回所有男员工记录
ResultSet rs=s.executeQuery("select no,name,sex,salary from employee where sex='男'");
System.out.println("no\t name\t sex\t salary");
//next方法,ResultSet 光标最初位于第一行之前;第一次调用 next 方法使第一行成为当前行;
//当调用 next 方法返回 false 时,光标位于最后一行的后面。任何要求当前行的 ResultSet 方法调用将导致抛出
SQLException。
while(rs.next()){
StringBuilder builder=new StringBuilder(rs.getString(1));
builder.append("\t");
builder.append(rs.getString(2));
builder.append("\t");
builder.append(rs.getString(3));
builder.append("\t");
builder.append(rs.getFloat(4));
System.out.println(builder.toString());
}
//添加记录
s.execute("insert into employee valus('2001','邢雪花','女',650)");
//修改salary
s.execute("update employee set salary=900 where no='2001'");
rs=s.executeQuery("select salary from employee where no='2001'");
rs.next();
float f=rs.getFloat(1);
System.out.println("no=2001员工修改后工资为:"+f);
//删除数据表
s.execute("drop table employee");
rs.close();
s.close();
conn.close();
try{
DriverManager.getConnection("jdbc:derby:;shutdown=true");
}catch(SQLException se){
System.out.println("Database shut down abnormally");
}
}catch(Throwable e){
e.printStackTrace();
}
System.out.println("Finished!");
}
}
运行时,总是说在第12行,创建数据表时出错,各位大神帮忙看看,是什么原因呢?
------解决方案--------------------
Caused by: ERROR 42X01: 语法错误:Encountered "no" at line 1, column 8。
把列名no换成其他的看看,也许no在这个数据库中是不允许使用的,但是no在sqlite中是没有问题的