Java Prepared Statement syntax to connect hive server for describing table - java

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

Related

Unable to connect to LAN server; no exception is shown

I have tried to use another proyect and the connection is succesfull but from this proyect I am only able to connect to localhost mysql. I want it to work only on lan.
I am getting "Access denied for user 'root'#'192.168.1.70' (using password: YES)"
Code sample from not working proyect:
Connection con = null;
Statement st = null;
ResultSet rs = null;
try{ con = (Connection) DriverManager.getConnection("jdbc:mysql://"+home.credentials[0],home.credentials[1],home.credentials[2]);
st = (Statement) con.createStatement();
String s = "SELECT * FROM meta";
rs = st.executeQuery(s);
ResultSetMetaData rsmt = rs.getMetaData();
while(rs.next()){
int meta = rs.getInt("meta");
goal.setText(Integer.toString(meta));
}
}catch(Exception e){}
finally{
try{ st.close();
rs.close();
con.close();
}
catch(Exception e){ JOptionPane.showMessageDialog(null, "InformaciĆ³n no encontrada");
}
}
Code sample from another proyect which connects succesfully
try
{
// create our mysql database connection
String myDriver = "org.gjt.mm.mysql.Driver";
String myUrl = "jdbc:mysql://192.168.1.66:3306/jjeventoscore";
Class.forName(myDriver);
Connection conn = (Connection) DriverManager.getConnection(myUrl, "root", "");
// our SQL SELECT query.
// if you only need a few columns, specify them by name instead of using "*"
String query = "SELECT * FROM client";
// create the java statement
Statement st = (Statement) conn.createStatement();
// execute the query, and get a java resultset
ResultSet rs = st.executeQuery(query);
// iterate through the java resultset
while (rs.next()){
String name = rs.getString("name");
String last = rs.getString("last");
String h_phone = rs.getString("h_phone");
String m_phone = rs.getString("m_phone");
String of_phone = rs.getString("of_phone");
String ex_phone = rs.getString("ex_phone");
String email = rs.getString("email");
String medium = rs.getString("medium");
System.out.println(name+" "+last);
}
st.close();
}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
You need to give privileges to the user on that Database and that Table.
With privileges, on your MySQL instance:
USE jjeventoscore;
GRANT ALL ON jjeventoscore.* TO 'root'#'192.168.1.70';
Or maybe try
GRANT ALL ON jjeventoscore.* TO 'root'#'192.168.1.70' IDENTIFIED BY '';
Since it says "Using password: YES"
Also, check the password on MySQL. It should match the parameter in this function
Connection conn = (Connection) DriverManager.getConnection(myUrl, "root", "");

When fetching data from tables it is showing this error Parameter index out of range(1>number of parameters)

String get_date = check_in_date.getText();
String get_customer_no = customer_no.getText();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rst = null;
try{
String driver ="com.mysql.jdbc.Driver";
String url ="jdbc:mysql://localhost:3306/hotel";
String userid ="root";
String password ="tushar11";
Class.forName(driver);
conn = DriverManager.getConnection(url,userid,password);
pstmt = conn.prepareStatement("select occupantdetails.customer_name,
hoteldetails.service_detail, hoteldetails.cab_no from
occupantdetails JOIN hoteldetails ON
occupantdetails.customer_no=hoteldetails.customer_no" );
pstmt.setString(1, get_customer_no);
rst = pstmt.executeQuery();
while(rst.next()){
txt_customer_name.setText(rst.getString("customer_name"));
txt_room_no.setText(rst.getString("service_detail"));
txt_cab_no.setText(rst.getString("cab_no"));
}
}
I am new to this. As i am fetching the details it is showing the parameter error and i cannot solve this. I think i have written the right query and there might be mistake in java code.
I guess your statement should be something like
"SELECT *
FROM YourTable
WHERE customer_no = ?"
Check the tutorial for prepared statements

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.

Prepared statement error when trying to receive password from DB

I get an error in this method:
public String databaseServer(String email)throws IllegalArgumentException {
Connection dbCon = null;
Statement stmt = null;
String password = null;
System.out.print(email);
String query = "SELECT password FROM user WHERE email = ?";
try {
dbCon = initializeDBConnection();
PreparedStatement preparedStatement = dbCon.prepareStatement(query);
preparedStatement.setString(1, email);
ResultSet rs = preparedStatement.executeQuery(query);
while (rs.next()) {
password = rs.getString("password");
}
} catch (SQLException ex) {
Logger.getLogger(Collection.class.getName())
.log(Level.SEVERE, null, ex);
}
//close connection ,stmt and resultset here
return password;
}
The error is:
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 '?' at line 1
I've checked the connection and the incoming string and they both work fine.
Anybody knows what I'm doing wrong?
Instead of
ResultSet rs = preparedStatement.executeQuery(query);
do
ResultSet rs = preparedStatement.executeQuery();

Java JDBC First () in ResultSet is not working?

I want to change the position of the cursor to the first row but I don't know why my code is not working.when I add rs2.first():
and also I am getting this error :
This method should only be called on ResultSet objects that are scrollable (type TYPE_SCROLL_INSENSITIVE).
try{
String driver = "org.apache.derby.jdbc.ClientDriver";
Class.forName(driver).newInstance();
String url = "jdbc:derby://localhost:1527/test";
Connection conn = DriverManager.getConnection(url);
String query = "select * from APP.RANKING";
Statement stmt = conn.createStatement();
Statement stmt2 = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSet rs2 = stmt2.executeQuery(query);
while (rs.next()){
String BID = rs.getString("BALLOT_ID");
String CN = rs.getString("CANDIDATE_NAME");
String ROID = rs.getString("USER_ID");
Ro1_ID = ROID;
String RA = rs.getString("RANK");
int rowNum = rs.getRow();
int rowNum2;
boolean In_check = false;
while(rs2.next()){
In_ballot.addElement(BID);
}
rs2.First();
In_ballot.addElement(BID);
}
}
catch(Throwable e) {
System.err.println(e.getMessage());
}
this.InB_list.setModel(In_ballot);
By default, calling createStament() in a connection results in every ResultSet having type 'TYPE_FORWARD_ONLY' - This results in the exception you see using first().
Instead, use another versions of createStatement, like this one.
This sample of creating scrollable ResultSets in Derby might help.

Categories