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".
Related
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
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?
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.
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();
}
What's wrong with this code? It gives me "brand" = null.
public String getBrand(#WebParam(name = "regNr") String regNr) throws SQLException {
String brand=null;
Connection con;
Statement st;
ResultSet rs;
String url = "jdbc:mysql://db4free.net:3606/cars";
String user = "cars";
String password = "";
try {
con = (Connection) DriverManager.getConnection(url, user, password);
st = (Statement) con.createStatement();
rs = st.executeQuery("SELECT * FROM Cars WHERE nr= '" + regNr + "'");
if (rs.next()) {
brand=rs.getString("brand");
}
} catch (SQLException ex) {
}
return brand;
}
I want to display the value of brand from database. Is it a problem with connection?
Either the query isn't returning any rows or a SQLException is being thrown. Add something to your catch block to discover what the exception is.
BTW: You should be calling close() on your ResultSet, Statement and Connection instances. I suggest putting those calls within a finally block. Additionally, your code is vulnerable to an attack called SQL injection. Use a PreparedStatement with parameters to repair this problem.
There probably is an exception in the code but you don't see it because of the catch with nothing inside. Add a log of the exception inside it, to reveal the exception and see the problem.