Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2016
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    need help with jsp error:

    I an getting an error when running this with a database:
    Connecting ...
    Connection established.
    Table created.
    com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY'

    here is the jsp code:
    Code:
    <%@page import="java.sql.*"%>  
    <%// Declare needed variables
    Connection con;
    DatabaseMetaData md;
    String qs;
    Statement stmt;
    ResultSet rs;
    	out.println("\n <br>");
    
    // Load DiverManager
    Class.forName("com.mysql.jdbc.Driver");
    	out.println("Connecting ...\n <br>");
    try {
    
    // Open Database connection
    con = DriverManager.getConnection("jdbc:mysql://db1.cs.uakron.edu:3306/random","random","random");
    	out.println("Connection established.\n <br>");
    stmt = con.createStatement(); // SQL statement object  
    
    //Create tables
    //qs="create table Students(sid integer, sname varchar(50), address varchar(50), phone_number integer, degree varchar(20), classes integer)";
    //stmt.executeUpdate(qs);
    	out.println("Table created.\n <br>");
    
    //Insert data
    qs="insert into Students (user_id,full_name,address,phone_number,degree,classes) values (1, 'Kyle', 'akron', 18 , 'pizza' , 4)";
    stmt.executeUpdate(qs);
    qs="insert into Students(user_id,full_name,address,phone_number,degree,classes) values (1, 'Kyle', 'akron', 18 , 'pizza' , 4)";
    stmt.executeUpdate(qs);
    qs="insert into Students (user_id,full_name,address,phone_number,degree,classes) values (1, 'Kyle', 'akron', 18 , 'pizza' , 4)";
    stmt.executeUpdate(qs);
    qs="insert into Students (user_id,full_name,address,phone_number,degree,classes) values (1, 'Kyle', 'akron', 18 , 'pizza' , 4)";
    stmt.executeUpdate(qs);
    	out.println("Data inserted.\n <br>");
    
    
    //Query the data
    qs="select * from Students";
    rs=stmt.executeQuery(qs);
    
    //Show results
    out.println("Records in the table: \n <br>");
    out.println("sid\t sname\t rating\t age\n <br>");
    while(rs.next())
    {
    out.println(rs.getString("sid")+"\t"+rs.getString("sname")+"\t"+rs.getString("rating")+"\t"+rs.getString("age"));
    out.println("<br>");
    }
    rs.close();
    
    /*
    //Get meta data
    md = con.getMetaData();
    // print information about the driver:
    //out.println("\nDriver name:" + md.getDriverName() + "\n<br>Driver version: " + md.getDriverVersion() +"\n<br>");
    ResultSet trs=md.getTables(null,null,null,null);
    String tableName;
    while(trs.next())
    {
        tableName = trs.getString("TABLE_NAME");
        System.out.println("Table: " + tableName);
        //print all attributes
        ResultSet crs = md.getColumns(null,null,tableName,null);
        while(crs.next()) out.println(crs.getString("COLUMN_NAME") + ", ");
        //crs.close();
    }
    //trs.close();
    */
    
    //Clean up
    out.println("Closing the session ...\n <br>");
    //Drope the tables
    qs="drop table Students";
    stmt.executeUpdate(qs);
    stmt.close();
    con.close();
    out.println("Session closed.\n <br>");
    }
    
    catch (Exception e) {
    out.println(e.toString());  // Error message to display
    }
    
    
    
    %>

  2. #2
    Senior Coder benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    1,144
    Thanks
    2
    Thanked 118 Times in 115 Posts
    Look at the data you are trying to insert. They all can't have the same id on a primary key. Increment each user_id. 1,2,3,4
    To save time, lets just assume I am almost never wrong.

    The XY Problem
    The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

    "This text has been encoded with ROT26. If you can read this you must have found a backdoor. Congratulations!"

  3. #3
    New to the CF scene
    Join Date
    Sep 2016
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    so like this?
    if(user_id == ""){
    user_id = "0";
    }

    else{
    ++user_id
    }

    //Create tables
    qs="create table Students(sid integer, sname varchar(50), address varchar(50), phone_number integer, degree varchar(20), classes integer)";
    stmt.executeUpdate(qs);
    out.println("Table created.\n <br>");

    //Insert data
    qs="insert into Students (user_id,full_name,address,phone_number,degree,classes) values (1, 'Kyle', 'akron', 18 , 'pizza' , 4)";
    stmt.executeUpdate(qs);
    qs="insert into Students(user_id,full_name,address,phone_number,degree,classes) values (1, 'Kyle', 'akron', 18 , 'pizza' , 4)";
    stmt.executeUpdate(qs);
    qs="insert into Students (user_id,full_name,address,phone_number,degree,classes) values (1, 'Kyle', 'akron', 18 , 'pizza' , 4)";
    stmt.executeUpdate(qs);
    qs="insert into Students (user_id,full_name,address,phone_number,degree,classes) values (1, 'Kyle', 'akron', 18 , 'pizza' , 4)";
    stmt.executeUpdate(qs);
    out.println("Data inserted.\n <br>");
    Last edited by kdrumz; 12-08-2016 at 03:27 AM.

  4. #4
    Senior Coder benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    1,144
    Thanks
    2
    Thanked 118 Times in 115 Posts
    NO! In your insert data.

    qs="insert into Students (user_id,full_name,address,phone_number,degree,classes) values (1, 'Kyle', 'akron', 18 , 'pizza' , 4)";
    stmt.executeUpdate(qs);
    qs="insert into Students(user_id,full_name,address,phone_number,degree,classes) values (1, 'Kyle', 'akron', 18 , 'pizza' , 4)";
    stmt.executeUpdate(qs);
    qs="insert into Students (user_id,full_name,address,phone_number,degree,classes) values (1, 'Kyle', 'akron', 18 , 'pizza' , 4)";
    stmt.executeUpdate(qs);
    qs="insert into Students (user_id,full_name,address,phone_number,degree,classes) values (1, 'Kyle', 'akron', 18 , 'pizza' , 4)";
    To save time, lets just assume I am almost never wrong.

    The XY Problem
    The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

    "This text has been encoded with ROT26. If you can read this you must have found a backdoor. Congratulations!"


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •