简体   繁体   English

如何在jdbc中插入行

[英]how to insert a row in jdbc

ok so I am trying to add a record to my database but it isn't working (hence why I'm here asking a question) 好的,所以我试图将记录添加到我的数据库中,但是它不起作用(因此为什么我在这里问一个问题)

no errors are being thrown... put a try catch in which executes fine so surely it should work? 没有引发任何错误。。。尝试尝试执行,这样肯定可以正常工作吗?

when I run the code the server also starts and I can call methods from another project that are stored on the server project so I know that the server is working 当我运行代码时,服务器也会启动,并且可以从存储在服务器项目中的另一个项目中调用方法,因此我知道服务器正在运行

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;
    }
}

Your code seems to have several erros, such as 您的代码似乎有一些错误,例如

  1. properties, what properties is that? 属性,那是什么属性?
  2. SQL is wrong SQL错误
  3. next time provide the table structure 下次提供表格结构
  4. print stacktrace instead of exception message and provide it for us 打印stacktrace而不是异常消息,并提供给我们
  5. do not print in the catch clause that you're connected when you're maybe not 当您可能不在时,不要在连接的catch子句中打印
  6. how the method throws an exception that is caught? 该方法如何引发捕获的异常?
  7. do not use a string as primary key, this is not efficient 不要使用字符串作为主键,这样效率不高

below there's a sample that works (although the table structure is probably too poor to be used outside an example like this) 下面有一个有效的示例(尽管表结构可能太差,无法在像这样的示例之外使用)

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM