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

Answers


Your code seems to have several erros, such as

  1. properties, what properties is that?
  2. SQL is wrong
  3. next time provide the table structure
  4. print stacktrace instead of exception message and provide it for us
  5. do not print in the catch clause that you're connected when you're maybe not
  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;
    }
}

Need Your Help

Making a reusable predicate for EntitySet<T>, IQueryable<T> and IEnumerable<T>

c# .net linq linq-to-sql

In my LINQ to SQL setup I have various tables which are mapped to classes which basically support the same interface to support versioning, i.e.