I am trying it from last 2 hour i dont know what is going wrong. also let me know how could i test if the connection is successfull or not.. it is for now.. but still i wanted to know..
Error:
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 '?, ?)'
My code:
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/medicinedb";
static final String USER = "root";
static final String PASS = "";
static final String DRIVER = "com.mysql.jdbc.Driver";
public boolean saveMedicine(List<MedicineName> medicineName) throws ClassNotFoundException {
Connection conn = null;
Statement stmt = null;
try {
Class.forName(DRIVER);
//STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
//STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
for (MedicineName element : medicineName) {
String sql;
sql = " insert into medicinename (name, pgurl)" + " values
( ?, ?)";
System.out.println(conn);
PreparedStatement preparedStmt = conn.prepareStatement(sql);
preparedStmt.setString(1, element.getName());
preparedStmt.setString(2, element.getPgurl());
stmt.executeUpdate(sql);
}
stmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
Why you are using Statement and Prepared statement. You are performing operations with Statement. Thus it does not have value. Remove the Statement
stmt = conn.createStatement();
And do the following
PreparedStatement preparedStmt = conn.prepareStatement(sql);
preparedStmt.setString(1, element.getName());
preparedStmt.setString(2, element.getPgurl());
//stmt.executeUpdate(sql);
preparedStmt .executeUpdate(sql);
use
String sql = "insert into medicinename (name, pgurl) values (?, ?)";
PreparedStatement preparedStmt = conn.prepareStatement(sql);
preparedStmt.setString (1, element.getName());
preparedStmt.setString (2, element.getPgurl());
preparedStmt.executeUpdate();
you should delete executeUpdate(sql); to executeUpdate();
You mix between your stm and preparedStmt, you have to execute preparedStmt and not stm.
Another thing when you want to executing multiple statements as one unit, its good to work with Statement Batching instead for example :
PreparedStatement preparedStmt;
String sql;
conn.setAutoCommit(false);
for (MedicineName element : medicineName) {
sql = "insert into medicinename (name, pgurl) values (?, ?)";
System.out.println(conn);
preparedStmt = conn.prepareStatement(sql);
preparedStmt.setString(1, element.getName());
preparedStmt.setString(2, element.getPgurl());
preparedStmt.addBatch(sql);
}
preparedStmt.executeBatch();
conn.commit();
Related
I got this problem in my code java.sql.Exception: Parameter index out of range ( 2 number of parameters,which is 1).
This is my code:
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t201835006","root","p73421231");
String mysql = "insert into books (bookName,Author,Perinting,Available) VALUES ('?','?',?,'True')";
PreparedStatement pstmt = conn.prepareStatement(mysql);
pstmt.setString(1,jTextField1.getText());
pstmt.setString(2,jTextField2.getText());
pstmt.setInt(3,Integer.parseInt(jTextField3.getText()));
pstmt.executeUpdate();
JOptionPane.showMessageDialog(null,"insertion successful");
I tried :
"insert into books VALUES ('?','?',?,'True')";
"insert into books VALUES (?,?,?,'True')";
but it didn'T work.
My other codes in same project includes same databases but it works. I want to share iw for you:
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t201835006","root","p73421231");
String sql ="insert into customerinfo values (?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1,jTextField3.getText());
pstmt.setString(2,jTextField1.getText());
pstmt.setInt(3,Integer.parseInt(jTextField2.getText()));
pstmt.setString(4,jTextField4.getText());
pstmt.executeUpdate();
JOptionPane.showMessageDialog(null,"insertion successful");
new Menu().setVisible(true);
dispose();
I don't know why the first code doesn't work help me.
I want to insert logical operations (=,<,>=,<,>=) in my sql
try {
String sql = "SELECT id,name,date_N FROM LECTEUR WHERE date_N ?,?";
Connection con = DBinfo.getConnection();
PreparedStatement ps = (PreparedStatement) con.prepareStatement(sql);
ps.setString(1, operationCB.getValue());
ps.setString(2, date1.toString());
ResultSet resultSet = ps.executeQuery();
output:
Incorrect syntax near '#P0'.
You can only insert values as parameters of the PreparedStatement.
If you want the operation to be dynamic, use a StringBuilder to build the query String:
StringBuilder sql = new StringBuilder();
sql.append("SELECT id,name,date_N FROM LECTEUR ");
sql.append("WHERE date_N ");
sql.append(operationCB.getValue());
sql.append(" ?");
Connection con = DBinfo.getConnection();
PreparedStatement ps = (PreparedStatement) con.prepareStatement(sql.toString());
ps.setString(1, date1.toString());
ResultSet resultSet = ps.executeQuery();
You cannot bind a logical operator to a prepared statement using a ? placeholder. One workaround would be to maintain separate queries for the various logical operations:
Map<String, String> queryMap = new HashMap<>();
queryMap.put("=", "SELECT id, name, date_N FROM LECTEUR WHERE date_N = ?");
queryMap.put(">", "SELECT id, name, date_N FROM LECTEUR WHERE date_N > ?");
// etc.
try {
String sql = queryMap.get(operationCB.getValue()); // fetch the appropriate statement
Connection con = DBinfo.getConnection();
PreparedStatement ps = (PreparedStatement) con.prepareStatement(sql);
ps.setDate(1, date1);
ResultSet resultSet = ps.executeQuery();
} catch (SQLException e) {
// handle exception
}
String user = request.getParameter("uname");
String pwd = request.getParameter("pass");
String pwd1 = request.getParameter("pass");
String pwd2 = request.getParameter("pass");
try{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/logindb";
Connection con = DriverManager.getConnection(url, "root", "root");
String sql = "select * from register";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, pwd);
ps.setString(2, request.getParameter("uname"));
ResultSet rs = ps.executeQuery();
if(rs.next())
{
sql = "update register set pass=? where uname=? ";
ps = con.prepareStatement(sql);
ps.setString(1, pwd);
ps.setString(2, request.getParameter("uname"));
ps.executeUpdate();
out.println("password changed");
}
}
catch(Exception e)
{
out.println(e);
}
This Is my code and it will show me an error like this
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).**
Here:
String sql = "select * from register";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, pwd);
ps.setString(2, request.getParameter("uname"));
You are trying to set parameters that aren't there. Looks like a copy-and-paste error. Those last 2 lines shouldn't be there.
Following is the code.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:cse");
//Statement stmt;
ResultSet rset;
//stmt = conn.createStatement();
String sql = " Select * from registration where id=?";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, "101");
rset = pst.executeQuery(sql);
while (rset.next()) {
arr.add(rset.getInt("id"));
arr.add(rset.getString("first"));
arr.add(rset.getString("last"));
arr.add(rset.getInt("age"));
}
System.out.println(arr);
pst.close();
conn.close();
For the above am getting "Error: java.sql.SQLException: Driver does not support this function". What might be the problem?
You are misusing the PreparedStatement interface. When using PreparedStatements, you should prepare the statement with your query, bind all necessary parameters and then execute it without any SQL - this will cause the statement to execute the previously prepared SQL statement:
String sql = "Select * from registration where id=?";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, "101");
rset = pst.executeQuery(); // Note - No args in the executeQuery call
I want to insert row into MySql database with a normal Program for Mysql connection in Java Netbeans,but when i run this code my database remains unaffected.I had setup connection with Netbeans and Mysql and it is working fine.
Code:
import java.sql.*;
public class MySqlConnection {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/migration";
static final String USER = "root";
static final String PASS = "ngts12345";
public static void main(String[] args) {
Connection conn = null;
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
System.out.println("Inserting records into the table...");
String sql = "INSERT INTO document (document_id, document_name, format)" +
"VALUES (?, ?, ?)";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setInt(1, 1);
preparedStatement.setString(2, "Test2");
preparedStatement.setString(3, "Test3");
preparedStatement.executeUpdate();
preparedStatement.close();
conn.close();
}catch(SQLException se){
//Handle errors for JDBC
}catch(Exception e){
//Handle errors for Class.forName
}
}
}
there is not space after format) and values
Change this line to
String sql = "INSERT INTO document (document_id, document_name, format)" +
"VALUES (?, ?, ?)";
to
String sql = "INSERT INTO document(document_id, document_name, format) " +
"VALUES (?, ?, ?)";
Try doing this:
preparedStatement.executeUpdate(sql);
and you should also specify database
String sql = "INSERT INTO database_name.document (document_id, document_name, format)" +"VALUES (?, ?, ?)";
try{
Class.forName("com.mysql.jdbc.Driver"); // MySQL database connection
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306","root","");
String sql = "select * from 'table_name'.database_name where user=? and pass=?";
pst = conn.prepareStatement(sql);
rs= pst.executeQuery();
}
Note preparedStatement.executeUpdate() returns true (record was successful in insert, update, or delete) or false (not successful). Therefore I suggest printing out the result to see if it worked.
As for not getting an error message, are you consuming exceptions and not printing out the error? In your catch block, put e.printStackTrace() to see what the error is.