import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
public class Create
{
public static void main( String [] args)throws Exception
{
Connection con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1522/orcl1","scott","sada");
System.out.println("connection is createad");
Statement stmt=con.createStatement();
System.out.println("statemnt of object is createad");
stmt.executeUpdate("create table iteam(iteamno number(3)primarykey,iteamname number(15),price number(4))");
System.out.println("table is created ");
con.close();
stmt.close();
System.out.println("conncetion closed");
}
}
Every thing executed successfully but after Statement object it will show this error when my program executes:
java.sql.SQLSyntaxErrorException: ORA-00907: missing right parenthesis
Error
A spacing error. Fixed below; you'd do well to try your statements in a fiddle before posting a question here, #sada:
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
public class Create
{
public static void main( String [] args)throws Exception
{
Connection con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1522/orcl1","scott","sada");
System.out.println("connection is createad");
Statement stmt=con.createStatement();
System.out.println("statemnt of object is createad");
stmt.executeUpdate("create table iteam(iteamno number(3) primary key,iteamname number(15),price number(4))");
System.out.println("table is created ");
con.close();
stmt.close();
System.out.println("conncetion closed");
}
}
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.
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();
}
}
}
I am trying to create a hive table using java.
Here is my code:
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
public class HiveCreateTable {
private static String driverName = "com.facebook.presto.jdbc.PrestoDriver";
public static void main(String[] args) throws SQLException {
// Register driver and create driver instance
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("haiiiiii");
Connection con = DriverManager.getConnection("jdbc:presto://192.168.1.100:8023", "", "");
con.setCatalog("hive");
con.setSchema("log");
Statement stmt = con.createStatement();
ResultSet res = stmt.executeQuery("create table access_log (c_ip varchar,cs_username varchar,cs_computername varchar,cs_date varchar,cs_code varchar,cs_method varchar,cs_uri_stem varchar,cs_uri_query varchar,cs_status_code varchar,cs_bytes varchar) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\b'LINES TERMINATED BY '\n'STORED AS TEXTFILE");
System.out.println("Table access_log4 created.");
ResultSet res1 = stmt.executeQuery("LOAD DATA LOCAL INPATH '/home/hadoop/access_log.txt'" + "OVERWRITE INTO TABLE access_log4;");
System.out.println("data loaded to access_log4.");
con.close();
}
}
and getting following error:
Exception in thread "main" java.sql.SQLException: Query failed
(#20150805_063004_00002_3dvaz): line 1:214: mismatched input 'ROW'
expecting
If we remove "ROW FORMAT DELIMITED FIELDS TERMINATED BY '\b'LINES TERMINATED BY '\n'STORED AS TEXTFILE" table is creating but data is not loaded.
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)