好吧所以我试图在我的数据库中添加一条记录,但它不起作用(因此我在这里问一个问题)
没有抛出任何错误...把一个try catch执行得很好,所以它肯定会起作用吗?
当我运行代码时,服务器也会启动,我可以调用存储在服务器项目中的另一个项目的方法,所以我知道服务器正在运行
public class RegisterUser {
public Connection getConnection() throws SQLException {
Connection conn = null;
Properties connectionProps = new Properties("admin", "adminadmin");
try {
conn = DriverManager.getConnection(
"jdbc:derby://localhost:1527/Social_Network");
Statement stmt = conn.createStatement();
int nbUpdatedResult = stmt.executeUpdate("INSERT INTO table (USERS) VALUES (INSERT INTO ADMINISTRATOR.USERS (USERID, USERNAME, PASSWORD, SEX, BIRTH, DEATH) \n"
+ " VALUES ('1', 'salems24', 'Twisted1@', 'M', '2014-01-24', '2014-01-25')");
} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println("Connected to database");
}
return conn;
}
}
答案 0 :(得分:0)
您的代码似乎有多个错误,例如
下面有一个有效的样本(虽然表格结构可能太差,无法在这样的例子之外使用)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class RegisterUser {
public static void main(String[] args){
new RegisterUser().getConnection();
}
public Connection getConnection(){
Connection conn = null;
Properties props = new Properties();
try {
conn = DriverManager.getConnection(
"jdbc:derby:derbyDB;create=true",props);
Statement stmt = conn.createStatement();
// stmt.executeUpdate("create table users (userid int not null primary key,username varchar(200), password varchar(200), sex varchar(200), birth date, death date)");
int nbUpdatedResult = stmt.executeUpdate("INSERT INTO USERS (USERID, USERNAME, PASSWORD, SEX, BIRTH, DEATH) VALUES (2, 'salems24', 'Twisted1@', 'M', '2014-01-24', '2014-01-25')");
System.out.println(nbUpdatedResult);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}