Using PreparedStament of JDBC with Mysql's TableName end with '\' - java

I am trying to insert something into my Mysql Database, which has a table whose name end with '\'. Its name is 'abc\'.
public static void main(String[] args) throws ClassNotFoundException{
try {
Class.forName("com.mysql.jdbc.Driver");
Connection master = DriverManager.getConnection($URL,$USR,$PWD);
String st = "insert into `abc\\` (`pad`,`c`,`k`,`id`) values(?,?,?,?)";
PreparedStatement pstmt = master.prepareStatement(st);
pstmt.setInt(1, 10);
pstmt.setInt(2, 1);
pstmt.setString(3, "1");
pstmt.setString(4, "2");
pstmt.execute();
master.close();
}catch(SQLException ex){
ex.printStackTrace();
}
But it doesn't work, the problem is :
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
But, it the table's name is 'a\bc', whose backslash is not the last character, my program works well.
Can anyone help me to solve this problem? Thanks a lot.

It is working with Statement.
public static void main(String[] args) throws ClassNotFoundException,
SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
String st = "insert into `abc\\` (`pad`,`c`,`k`,`d`) values(10,11,'5','6')";
Statement stmt = (Statement) con.createStatement();
stmt.execute(st);
con.close();
}

Related

SQL Exception cannot issue statements that do not produce result sets- Java

public static void main(String[] args) throws Exception{
Connection con = DriverManager.getConnection(url, uname, pass);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);
String name = rs.getString("NAME");
System.out.println(name);
st.close();
con.close();
}
: When I run this code this is the error i am getting..
Exception in thread "main" java.sql.SQLException: Statement.executeQuery() cannot issue statements that do not produce result sets.
What can I do to fix this error?
In my case the solution was to downgrade from "mysql-connector-java-8.0.30" to "mysql-connector-java-8.0.18".

Java Prepared Statement syntax to connect hive server for describing table

trying to establish connection to hive and describe a table.
enter code here
String queryTable= "dummydb.dummy_table";
String driverName = "org.apache.hive.jdbc.HiveDriver";
try {
Class.forName(driverName);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
//connect hive server (no issue in that)
String hiveUrl = PropertyLoader.getInstance("staging").getDruidHive();
Connection con = DriverManager.getConnection(hiveUrl, "hadoop", "");
##Working code is below:
String sql = "describe formatted dummydb.dummy_table" ;
PreparedStatement pstmt = con.prepareStatement(sql);
ResultSet rs1 = pstmt.executeQuery();
##But I try to use this which is not working:
String queryTable="dummydb.dummy_table"
String sql1 = "describe formatted ?" ;
PreparedStatement pstmt = con.prepareStatement(sql1);
pstmt.setString(1, queryTable);
ResultSet rs1 = pstmt.executeQuery();
Error
org.apache.hive.service.cli.HiveSQLException: Error while compiling statement: FAILED: ParseException line 1:19 cannot recognize input near 'formatted' ''dummydb.dummy_table'' '' in describe statement

How to use placeholder inside sql replace() in java?

I want to use placeholder inside sql replace function in java.
Following is the code.
import java.sql.*;
public class TestDB {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("class");
Connection con = DriverManager.getConnection("url","username","password");
PreparedStatement ps = null;
String query = "select name from image where id >= replace(?,'','','''') and id <= replace(?,'','','''') and name like ''%aa'' order by id;";
ps= con.prepareStatement(query);
ps.setString(1, "1");
ps.setString(2, "10");
ResultSet rs = null;
rs= ps.executeQuery();
while(rs.next())
{
System.out.println(rs.getString(1));
}
}
}
In ps= con.prepareStatement(query); line I am getting:
Exception in thread "main" java.sql.SQLException: An illegal character has been found in the statement.
Actually using a simple create statement it works fine. But when I use preparedstatement with placeholder it's throwing exception.
Can anyone let me know what the issue is?

Insert multiple data (List) in MySQL using JDBC

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();

Issue in connecting MS SQL server using Java program

I am having a problem in executing a simple select statement in Microsoft Sql Server DB using a Java program. I am facing below 2 issues,
Code by using statement * Resultset - program just hungs near the executequery() method and no progress after that.
Sample code:-(Full code pasted below)
Statement sta = conn.createStatement();
String Sql3 = "select name from dbo.network";
sta.executeQuery(Sql3);
ResultSet rs=sta.getResultSet();
code with preparestatment * Resultset throws error saying
The method executeQuery() cannot take arguments on a PreparedStatement or CallableStatement
Sample Code:-
PreparedStatement ps = conn.prepareStatement(Sql3);
ResultSet rs = ps.executeQuery(Sql3);
Complete Code:-
public class DbConnectionTest {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
System.out.println("Entered into main method");
String userName ="***";
String password ="***";
String url ="jdbc:sqlserver://****:1433;databaseName=***";
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(url, userName, password);
System.out.println("test");
Statement sta = conn.createStatement();
String Sql3 = "select name from dbo.network";
//PreparedStatement ps = conn.prepareStatement(Sql3);
//ResultSet rs = ps.executeQuery(Sql3);
sta.executeQuery(Sql3);
//sta.getMoreResults();
//sta.getMoreResults();
ResultSet rs=sta.getResultSet();
if(rs != null)
while (rs.next()) {
System.out.println(rs.getString("name"));
}
}catch(Exception se){
//Handle errors for JDBC
se.printStackTrace();
}
}
}
In Addition, I confirm the below,
Same query executes fine in the database.
SQL Browser Server service is running fine
TCP/IP is enabled
Credentials and the URL string are correct
sqljdbc4.jar is used.
Please help me with this issue.

Categories