Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java Databases error Exception in thread "main" java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?, ?, ?, ?, ?, ?, ?,

  1. #1
    Junior Member
    Join Date
    Nov 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Java Databases error Exception in thread "main" java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?, ?, ?, ?, ?, ?, ?,

    note: I use mysql database if that's of relevance

    Here's the code:
    Can I know how to fix the error

    package databaseTest;

    // import package
    import java.sql.*;
    public class DemoClass {

    public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub

    // load package from properties then add mysql jar drive
    String url = "jdbc:mysql://localhost:3306/coursesdatabase";
    String uname = "root";
    String pass = "fatima2002";

    String query = "select * from courseslist";
    // register driver
    Class.forName("com.mysql.cj.jdbc.Driver");
    // establish the connect
    Connection con = DriverManager.getConnection(url, uname, pass);
    // create statement
    Statement st = con.createStatement();

    // execute query
    ResultSet rs = st.executeQuery(query);

    String userData=null;

    while(rs.next()) { // how to fetch the whole table
    // shift pointer to next result
    userData = rs.getInt(1) + " : " + rs.getString(2);

    // process results
    System.out.println(userData);
    }

    String query2 = "insert into courseslist values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    int count = st.executeUpdate(query2); // affects a row
    System.out.println(count + "row/s affected");

    int classcode = 24590;
    String courseCode = "GENS";
    String courseName = "Arabic Language";
    int crdhrs = 3;
    int sectionNum = 1;
    String days = "TRU";
    String time = "11:00-11:50";
    String instructor = "Hakim Ajhar";
    int capacity = 40;
    String SemSrtEnd = "28/08-20/12";
    String roomNum = "G098549";

    PreparedStatement pSt = con.prepareStatement(query2);
    pSt.setInt(1, classcode);
    pSt.setString(2, courseName);
    pSt.setString(3, courseCode);
    pSt.setInt(4, crdhrs);
    pSt.setInt(5, sectionNum);
    pSt.setString(6, days);
    pSt.setString(7, time);
    pSt.setString(8, instructor);
    pSt.setInt(9, capacity);
    pSt.setInt(10, 0);
    pSt.setInt(11, 0);
    pSt.setString(12, SemSrtEnd);
    pSt.setString(13, roomNum);

    int count2 = pSt.executeUpdate();
    System.out.println(count2 + "row/s affected");
    // close
    st.close();
    con.close();

    }

    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Databases error Exception in thread "main" java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?, ?, ?, ?, ?, ?

    Sorry, I do not know SQL. Try asking your question here: http://www.coderanch.com/forums

    Posted here: https://coderanch.com/t/747306/datab...-MySQL-version
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 6
    Last Post: August 16th, 2014, 01:34 AM
  2. Replies: 1
    Last Post: April 7th, 2013, 03:40 PM
  3. Replies: 0
    Last Post: June 23rd, 2012, 12:57 AM
  4. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM