I try to run the query using the Java and below mentioned is my program
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.spi.DirStateFactory.Result;
public class Oracleconn {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:yglobal","user","user");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select empid from empmaster");
while(rs.next())
System.out.println(rs.getint(1));
con.close();
} catch (Exception e) {
e.printStackTrace();
}}
}
But when i tried to run this program I am getting the exception
java.sql.SQLException: ORA-01756: quoted string not properly terminated
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:743)
at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:207)
at oracle.jdbc.driver.T4CStatement.executeForDescribe(T4CStatement.java:790)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1038)
at oracle.jdbc.driver.T4CStatement.executeMaybeDescribe(T4CStatement.java:830)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1133)
at oracle.jdbc.driver.OracleStatement.executeQuery(OracleStatement.java:1273)
at jdbc.Oracleconn.main(Oracleconn.java:28)
Related
I try to insert record without using insert query, but I get:
SQLException - Invalid operation for read only resultset: moveToInsertRow.
I've attached my code:
package com.phoenix.demos;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class RandomAccessData {
public static void main(String[] args) {
//Class.forName("oracle.jdbc.driver.OracleDriver");
try {
Connection con = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:XE","system","deepa1994");
String query ="Select * from users";
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(query);
rs.next();
rs.last();
rs.next();
rs.absolute(4);
rs.previous();
rs.relative(-2);
System.out.println(rs.getString(1));
//To add a record - here i get error**
rs.moveToInsertRow();;
rs.updateString(1,"B");
rs.updateString(2, "C");
rs.insertRow();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I attached output screen to understand error message:
I am very new to Java and am simply trying to connect to my MSSQL database and return a list of customers. When I try the JDBC connection, I get a "no suitable driver found" error. When I add a Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver") statement, I get a ClassNotFound error. This seems like it should be a lot easier than it's turning out to be. Please help me either find a suitable driver or how to get access through the Class.forName usage.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Statement;
public class DbConn {
public static String getConnString(){
return "jdbc:sqlserver://localhost\\SQLEXPRESS:1433;database=OhHold;";
}
public static void getConnection() {
try
{
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String user = "<USER>";
String pw = "****************";
Connection connection = DriverManager.getConnection(getConnString(), user, pw);
Statement statement = connection.createStatement();
String sql = "select txtCompanyName as company from tblCustomers where intNotActive <> 1";
ResultSet result = statement.executeQuery(sql);
while (result.next()) {
System.out.println(result.getString(1));
}
}
/*
// Handle any errors that may have occurred.
catch (ClassNotFoundException e) {
e.printStackTrace();
}
*/
catch (SQLException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
getConnection();
}
}
I can't manage to get this right. I'm not sure of what is wrong. Apparently the connection is ok but can't get the result back of the query.
package probandoCouch;
import cdata.jdbc.couchbase.CouchbaseDriver;
import java.sql.Statement;
import java.util.Properties;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class App {
public static void main(String[] args) {
try {
Connection conn = DriverManager.getConnection(
"jdbc:couchbase:User=\"Administrator\";Password=\"Administrator\";Server=\"127.0.0.1\";");
Statement stat = conn.createStatement();
boolean ret = stat.execute("SELECT message FROM greeting WHERE author='foo';");
if (ret) {
ResultSet rs = stat.getResultSet();
while (rs.next()) {
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
System.out.println(rs.getMetaData().getColumnName(i) + "=" + rs.getString(i));
}
}
}
} catch (SQLException e) {
}
}
}
Can you please remove semicolon(;) from query and try
SELECT message FROM greeting WHERE author='foo';
Updated Query:
SELECT message FROM greeting WHERE author='foo'
With semicolon jdbc will fail with error
java.sql.SQLException: ORA-00933: SQL command not properly ended
In your code you may print the exception to see if any exception is thrown.
The code is deployed on JBOSS server sitting on LINUX Environment. Below is code to access the CLOB data from oracle using JDBC API.
JDBC jar used is ojdbc6.jar.
JAVA version : jdk1.8.0_221-amd64
Oracle java verion : 1.8.0_121
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
public class GetGenericPDFData {
public String GetPDFData(String SolutionID) throws IOException {
String PDFData = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:#ServerIP:1521:SID","username","password");
String Query = "select pdf_data from stage1.pdf_queue where system_id in (select system_id from (select * from pdf_queue where solution_id = ?
and status = 'Completed' order by time_stamp desc) where rownum=1)";
PreparedStatement preparedStatement = con.prepareStatement(Query);
preparedStatement.setString(1, SolutionID);
ResultSet rs = preparedStatement.executeQuery();
while(rs.next()){
PDFData = rs.getString("pdf_data");
}
//Below data is coming out as null
System.out.println(PDFData);
con.close();
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
System.out.println("GetGenericPDFData.GetPDFData 7");
e.printStackTrace();
PDFData = "false";
}
return PDFData;
}
}
I recently started working on JDBC and I got an Error as Table not found
Table TRIAL not found; SQL statement:
insert into trial values(1,'hello') [42102-73]
Here is my code, also I have already created a table in h2 DB
package trial1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Customer {
public static void main(String[] args) {
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:h2:~/test","Aziz", "password");
Statement st = conn.createStatement();
String str = "insert into trial values(1,'hello')";
st.execute(str);
System.out.println("Succes");
}
catch(Exception e) {
e.printStackTrace();
}
}
}