Java 如何在jdbc中插入行
好的,我正在尝试向数据库中添加一条记录,但它不起作用(因此我在这里问一个问题) 没有抛出任何错误。。。放一个执行良好的试捕器,这样它肯定会工作吗 当我运行代码时,服务器也会启动,我可以从存储在服务器项目上的另一个项目调用方法,这样我就知道服务器正在工作Java 如何在jdbc中插入行,java,netbeans,jdbc,Java,Netbeans,Jdbc,好的,我正在尝试向数据库中添加一条记录,但它不起作用(因此我在这里问一个问题) 没有抛出任何错误。。。放一个执行良好的试捕器,这样它肯定会工作吗 当我运行代码时,服务器也会启动,我可以从存储在服务器项目上的另一个项目调用方法,这样我就知道服务器正在工作 public class RegisterUser { public Connection getConnection() throws SQLException { Connection conn = null;
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;
}
}
您的代码似乎有几个错误,例如
那个SQLINSERT语句看起来很奇怪,实际上是无效的。如果您看到“已连接到数据库”,您有一个错误。它有什么无效之处?仅仅说它是无效的并没有多大帮助,没有显示我使用了这个抱歉,我的意思是“连接到数据库”没有显示该语句没有遵循标准的SQL插入语法。但是,第二次插入是有意义的:
插入ADMINISTRATOR.USERS(用户名、用户名、密码、性别、出生、死亡)值('1'、'salems24'、'Twisted1@'、'M'、'2014-01-24'、'2014-01-25')
。但您有两个INSERT语句混合在一起。好了,现在显示这些警告警告:跳过条目,因为它不是绝对URI。2014年1月26日下午2:45:40 com.sun.enterprise.glassfish.bootstrap.osgi.BundleProvisioner$DefaultCustomizer getLocations警告:跳过条目,因为它不是绝对URI。严重:SEC5054:证书已过期:请使用您的jdbc字符串,而不是我的。
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;
}
}