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:
Related
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.
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
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)
I want to store the count value received into a variable count which then can be appended to a string to prepare a customer id but it is giving me the cursor invalid error. Please have a look below and help me out. thanks in advance. :)
**package components;
import java.awt.geom.GeneralPath;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class genrateid {
public void generateid(){
int count;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:XE",
"system", "tiger");
System.out.println("Connection Successfull");
System.out.println(conn);
//--------------------------------------------------------------------
Statement stmt = conn.createStatement();
String query1= "select count(*) as customercount from customers";
ResultSet rs= stmt.executeQuery(query1);
count = rs.getInt(1);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
genrateid gen = new genrateid();
gen.generateid();
}
}**
I am receiving the following error in in eclipse IDE:
OUTPUT:
Connection Successfull
sun.jdbc.odbc.JdbcOdbcConnection#39b27b
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLGetDataInteger(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getDataInteger(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getInt(Unknown Source)
at components.genrateid.generateid(genrateid.java:29)
at components.genrateid.main(genrateid.java:43)
insert this line
if(rs.next())
before
count = rs.getInt(1);
and you'll be good. But, as Dems said, consider using IDENTITY column (or because you are using Oracle - SEQUENCE)