We have a database code in place for a long time which is running successfully with Progress DB. Recently, we tried it with SQL Server 2008 with JDBC 4 driver .It gave below exception:
Database '%' does not exist. Make sure that the name is entered correctly.
DatabaseMetaData conMD = connection.getMetaData();
ResultSet columns = conMD.getColumns("%", "%", m_Table, "%");
Could anybody please help me out ?
If you get the list of catalogs before?
With the method getCatalogs()
http://download.oracle.com/javase/1.4.2/docs/api/java/sql/DatabaseMetaData.html#getCatalogs()
As the error message says: you cannot pass a wildcard for the database (catalog) parameter. Wildcards are only allowed for the schema, table and column names.
Use null instead.
conMD.getColumns(null, "%", m_Table, "%");
conMD.getColumns(connnection.getCatalog(), "%", m_Table, "%");
instead of giving % ,connnection.getCatalog()will fix the issue.Interesting thing is that the code works with Oracle,MySql and Progress . Only Sql Server is throwing the error.
Related
I have a very simple SQL query I need to run
SELECT `id`, `{Document id}` FROM `test`.`test` LIMIT 10;
where {Document id} is a column name. Whenever I run it through MariaDB JDBC, it fails with error unknown escape sequence. From my understanding {CALL ...} is used to call stored procedure with a JDBC CallableStatement.
How do I escape it? I want JDBC to treat it as literal string without special meaning. \ didn't work for me.
As mentioned in deleted answer by #a_horse_with_no_name, there is setEscapeProcessing. But it's not supported by a lot of connectors (example MariaDB).
I have confirmed this issue using mariadb-java-client-2.2.5. It is not an issue with mysql-connector-java-5.1.44 so you might want to report this issue to MariaDB.
Following the suggestion by #JoopEggen in the comments above, I got it to work with MariaDB JDBC by adding sessionVariables=sql_mode=ANSI_QUOTES to the connection string and using
ResultSet rs = st.executeQuery("SELECT \"{Document id}\" FROM test.test");
![enter image description here][1]I am trying to update data in table of MySQL database.
I am unable to do, since I am beginner so I have no idea how to do , kindly guide me properly.
I would be thankful to you..............
Part of cade that generates exception:
java.sql.PreparedStatement statement = conection.prepareStatement("UPDATE patient_details set `Reg_Date`='?', `Name`='?', `Father_Husband_Name`='?', `Address`='?', `City`='?', `Cell_No`='?', `Martial_Status`='?', `Gender`='?', `Status`='?', `Age`='?' where 'Reg_No'='temp'");
statement.setInt(1, temp);
statement.setString(2,textField_3.getText());
statement.setString(3,textField_1.getText());
statement.setString(4,textField_2.getText());
statement.setString(5,textArea.getText());
statement.setString(6,textField_4.getText());
statement.setString(7,textField_5.getText());
statement.setString(8,(String) comboBox.getSelectedItem());
statement.setString(9,(String) comboBox_1.getSelectedItem());
statement.setString(10,(String) comboBox_2.getSelectedItem());
statement.setInt(11,temp1);
statement.executeUpdate();
Exception is:
parameter out of range(1>number of paarameters which is 0)
YOu set 11 parameters but only have 10 ? in the statement. The last part of the statement, the where clause - you have
where 'Reg_No'='temp'"
which is not a variable. You should be able to drop the statement
statement.setInt(11,temp1);
what I'm trying to do is get a ResultSet from a SQL statement from which then I can derive certain values. What I seem to be having troubles with is the preparing of the prepared statement which occurs at this line.
String sql = "SELECT " + dataState + " FROM " + table + whereState + ";";
java.sql.PreparedStatement prep = conn.prepareStatement(sql); // Error here
I've already checked that the variable conn is not null. All the values in the statement are not null and the stack trace shows this.
java.lang.NullPointerException
at org.sqlite.PrepStmt.(PrepStmt.java:37)
at org.sqlite.Conn.prepareStatement(Conn.java:231)
at org.sqlite.Conn.prepareStatement(Conn.java:224)
at org.sqlite.Conn.prepareStatement(Conn.java:213)
at org.utilities.storagemethods.SQLiteMethod.load(SQLiteMethod.java:223)
at org.utilities.DataManager.load(DataManager.java:97)
at org.utilities.Test.main(Test.java:21)
A print of the sql statement shows that it gives this.
SELECT testString, testBool, testObj FROM testTable WHERE testInt=?;
You can ascertain the values from the given string.
What I can't figure out is what I'm doing wrong to cause this, if you're curious about what sqlite system I'm using, you can find the home page here: http://www.zentus.com/sqlitejdbc/
Thanks in advance for any help on this. I'm completely stumped.
Note: In an attempt to save resources, I was caching my Connection's, and re-using them later. When I try re-getting the connection, it works perfectly. Is there a way I can cache them or do I need to load them every time I want to use it?
I was getting this error, and it was caused because I had closed the connection. The connection wasn't null, it was just closed. ARRRGGG! Like the OP hinted at, I had started with the sample code at Zentus (Xerial now, since Zentus seems to have shut down). The sample code has a finally block where he closes the connection. Once I took out the finally block, my error disappeared. Just remember to close the connection before you exit the program.
To see if you have the same problem, right before the line that gives the error, try adding
System.out.println("isClosed = " + conn.isClosed());
If not wrong, I think it is because of ';' inside your SQL statement. Please remove and try again ?
Regards,
Code Behind
Currently i'm writing a JDBC application to manage a MySQL database. I have the delete, insert and select methods functioning with the correct queries. I'm having trouble with the Update method. When using using the following code I receive a MySQL 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 "",Street",Town",City",PostCode",Age",email",RunningFee'false'Where PID=" at line 1...
private void updateData()
{
Connection con;
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost/snr","root","");
String sql = "Update participant Set password='"+txtpassword.getText()+"'," +
"lastName='"+txtlastName.getText()+"',firstName='"+
txtfirstName.getText()+"',HouseNumber'"+txtHouseNumber.getText()+"',Street'"+txtStreet.getText()+"',Town'"+txtTown.getText()+"',City'"+txtCity.getText()+"',PostCode'"+txtPostCode.getText()+"',Age'"+txtAge.getText()+"',email'"+txtemail.getText()+"',RunningFee'"+cbRunningFee.isSelected()+"' Where PID='"+txtPID.getText()+"'";
Statement statement = con.createStatement();
statement.execute(sql);
createMessageBox("Updated Successfully");
clearControls();
}
catch(Exception e)
{
createMessageBox(e.getMessage());
}
}
Is there something wrong with my SQL query?
Yes, your query is wrong. You're missing = on a great big bunch of set column/value pairs.
(And please consider using prepared statements and bind variables, SQL injection is just not something you want to be open to.)
Yes there is something wrong with the query. Your way of building query is vulnerable to SQL Injection. Use Parameterized Queries instead of concatenating text like that.
Read this article: Preventing SQL Injection in Java
Not only is your query incorrect, but it may also open you to SQL Interjection Attacks.
You need to parameterize your query by replacing the pasted-in values with question marks, preparing the statement, and executing it. See the tutorial that I linked.
Finally, storing a password as plain text is a very, very bad idea.
String sql = "UPDATE participant SET "+
"password=?, lastName=?, firstName=?, HouseNumber=?, Street=?, Town=?, "+
"City=?,PostCode?,Age=?,email=?,RunningFee=? "+
"WHERE PID=?";
PreparedStatement upd = con.prepareStatement(sql);
upd.setString(1, txtpassword.getText());
upd.setString(2, txtlastName.getText());
// ... and so on
upd.executeUpdate();
con.commit();
You are forgetting some = in your query.
Try
String sql = "Update participant Set password='"+txtpassword.getText()+"'," +
"lastName='"+txtlastName.getText()+"',firstName='"+
txtfirstName.getText()+"',HouseNumber='"+txtHouseNumber.getText()+"',Street='"+
txtStreet.getText()+"',Town='"+txtTown.getText()+"',City='"+txtCity.getText()+
"',PostCode='"+txtPostCode.getText()+"',Age='"+txtAge.getText()+"',email='"+
txtemail.getText()+"',RunningFee='"+cbRunningFee.isSelected()+
"' Where PID='"+txtPID.getText()+"'";
The error 'you have an error in your SQL syntax' is from the sql server and indicates that yes, you do have an error in your query. In these cases I often find it useful to print the constructed query itself, just to check that it is being constructed correctly.
In your case I believe the problem is that you are missing a bunch of "="s, you also probably need to escape your single quotes in the java so they are passed through correctly (replace ' with \').
i written this query (in java class) to select some information from the MySQL database
and view it on jsp page...
SELECT instructor.name FROM
instructor,section,teach WHERE
teach.student_id='3'AND teach.section
= section.number AND section.instructor_id= instructor.ID
but there is exception was occur!
javax.servlet.ServletException:
com.mysql.jdbc.exceptions.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
'.student_id='3'AND teach.section =
section.number AND
section.instructor_id= ins' at line 1
,,
by the way, i was write it in PhpMyAdmin, and it's work..
App server: Sun GlassFish Enterprise Server v2.1
please help me...
Regards
You need a space after the '3'.
It seems that there is a space missing. '.student_id='3'AND -> '.student_id='3' AND
If your query is exactly as you show it:
SELECT instructor.name FROM instructor,section,teach WHERE teach.student_id='3'AND
teach.section = section.number AND section.instructor_id= instructor.ID
then you need a space after the '3' and before AND on the first line shown above.
What is the datatype of "teach.student_id"? Is it numeric or varchar. If it is numeric, no need to put the '3' inside single quotes.
e.g.
teach.student_id = 3
Thanks for all
i was write query in java class like this
ResultSet rs
=stmt.executeQuery("SELECT instructor.name " +
"FROM instructor,section,teach" +
"WHERE teach.student_id=" + "'" + idd + "'" +
" AND teach.section = section.number
AND section.instructor_id=
instructor.ID");
then i eliminate all lines, and put query in one line like this, then it's solved...
Regards