CData JDBC Couchbase Java app connection problem - java

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.

Related

JDBC SQL Server errors: ClassNotFound & Not Suitable Driver Found

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

Unable to get column name and column lable in sql server result set meta data

Can anybody help me to get actual column name and its alias using ResultSetMetaData by connecting to sqlserver database.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
class MysqlCon {
public static void main(String args[]) {
Connection con = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=alcoa", "sa", "test");
Statement stmt = con.createStatement();
String query = "SELECT std_code as \"Student code\" from Student ";
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsm =rs.getMetaData();
for (int i = 1; i <= rsm.getColumnCount(); i++) {
System.out.println(rsm.getColumnLabel(i) + "--" + rsm.getColumnName(i));
}
} catch (Exception e) {
System.out.println(e);
} finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
output
Student code--Student code
Above code works as expected in other database like mysql and oracle.
I checked code for SQLServerResultSetMetaData. Both methods getColumnLabel and getColumnName are identical.
abzycdxw65, has raised same issue on their github account. it is closed.
Is there any way to get following output:
Student code--std_code
If I get your question you can try the followings to retrieve column names
select name from sys.columns

Database change notification in oracle 12c

I am using ojdbc8 12.2.0.1 in my maven project and I want to notify when there is a new entry in a table.
I was using ojdbc7 11.2.0.1 and oracle 11g and on that, it was working fine.
Now I am upgrading to oracle 12c for which I am using ojdbc8 12.2.0.1 jar.
The table is getting in the user_change_notification_regs table as I can see it but as soon as there is a new entry in the registered table, the registered table getting unregistered.
This means there is no entry in the user_change_notification_regs table.
The code is not giving any type of exception or error.
Any alternative of DCN would also help.
Code:
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import oracle.jdbc.OracleConnection;
import oracle.jdbc.OracleDriver;
import oracle.jdbc.OracleStatement;
import oracle.jdbc.dcn.DatabaseChangeEvent;
import oracle.jdbc.dcn.DatabaseChangeListener;
import oracle.jdbc.dcn.DatabaseChangeRegistration;
public class DBChangeNotification {
public static void main(String[] argv) {
try {
Connection con = DriverManager.getConnection(url, user, password);
startDcn(con);
} catch (SQLException mainSQLException) {
mainSQLException.printStackTrace();
}
}
public static void startDcn(Connection conn) throws SQLException {
Properties prop = new Properties();
prop.setProperty(OracleConnection.DCN_NOTIFY_ROWIDS, "true");
prop.setProperty(OracleConnection.DCN_IGNORE_DELETEOP, "true");
prop.setProperty(OracleConnection.DCN_IGNORE_UPDATEOP, "true");
DatabaseChangeRegistration dcr = conn.registerDatabaseChangeNotification(prop);
//listener not getting called as table getting unregistered.
dcr.addListener(new DatabaseChangeListener() {
public void onDatabaseChangeNotification(DatabaseChangeEvent dce) {
RowChangeDescription[] rowChangeDescription = dce.getTableChangeDescription()[0].getRowChangeDescription();
for (RowChangeDescription rcd: rowChangeDescription) {
System.out.print("Working"); //not getting print
}
}
});
Statement stmt = conn.createStatement();
((OracleStatement) stmt).setDatabaseChangeRegistration(dcr);
ResultSet rs = stmt.executeQuery("select * from demo where rownum='1' ");
while (rs.next()) {}
String[] tableNames = dcr.getTables();
for (int i = 0; i < tableNames.length; i++)
System.out.println(tableNames[i] + " is part of the registration.");
rs.close();
stmt.close();
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
Note: kindly ignore syntax error if any.

Table not found error in eclipse IDE, what to do?

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

Getting exception in JDBC thin driver

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)

Categories