1

I have this code:

import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.Statement;


public class DBConntest {

    public static void main(String[] args) {
    try {

        Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/?user=root&useSSL=false", "root", "root");
        System.out.println("Connection succes!");
        Statement stmt = conn.createStatement();
        String query1 = "USE test; insert into dbtester(name,age,email) values ('Aram',18,'[email protected]');";
        stmt.executeUpdate(query1);

    }
    catch (Exception e){
        System.err.println(e);;
    }

    }

}

I use Eclipse, and after run it return this error com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'insert into dbtester(name,age,email) values ('Aram',18,'[email protected]')' at line 1

Same SQL Query work in MySQL workbench. How I can solve this problem?

2
  • Change the line to String query1 = "insert into dbtester(name,age,email) values ('Aram',18,'[email protected]');"; Commented Feb 28, 2016 at 8:08
  • java.sql.SQLException: No database selected Commented Feb 28, 2016 at 8:43

1 Answer 1

0

Change the line to String query1 = "insert into dbtester(name,age,email) values ('Aram',18,'[email protected]');";

For no database selected exception : Your URL for your database should include your database name. This is normally your URL followed by a "/databasename".

String url = "jdbc:mysql://localhost/databasename";
String username = "test";
String password = "test";
Connection connection = DriverManager.getConnection(url, username, password);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.