jdbc type1 program error - java

There is no compile time error but runtime error is there. i have assigned the DSN by going to controlPanel->administrative tools->Data sources->Add button->select oracle in XE->finish-> DSN=oradsn id=system ->ok ->ok
but it is not still running. please help me out
/**
* Program1.java
* This program Creates table student
* columns: SID, SNAME, MARKS
* driver: Type-1(JDBC-ODBC Bridge)
* Database: Oracle 11g
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
class Program1{
public static void main(String[] args)throws Exception {
//Step-1: Load driver class
Class.forName("sun.jdbc.odbc.jdbcOdbcDriver");
System.out.println("Driver is loaded");
//step-2: open connection
Connection con=DriverManager.getConnection("jdbc:odbc:oradsn","system","tiger");
System.out.println("connection is opened");
//step-3: create statement object
Statement stmt=con.createStatement();
System.out.println("statement object created");
//prepare command
String cmd="create table faculty(SID number(5) primarykey, SNAME varchar(10), MARKS number(5))";
//step-4:execute command
int i=stmt.executeUpdate(cmd);
//step-5: print result
System.out.println("Table is created");
//step-6: close connection
stmt.close();
con.close();
System.out.println("connection closed");
}//main
}//Program1

Related

Connect Oracle Java stored procedure to Snowflake

I was trying to have snowflake db connection using oracle Java stored procedure. But it's giving me error
ORA-29532: Java call terminated by uncaught Java exception: java.sql.SQLException: No suitable driver found
I have already downloaded snowflake-jdbc-3.6.19-javadoc.jar from related source.
Below is sample code of java class which will be called from oracle java procedure.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class SnowflakeDriverExample
{
public static void testdata() throws Exception
// public static void main(String[] args) throws Exception
{
// get connection
System.out.println("Create JDBC connection");
Connection connection = getConnection();
System.out.println("Done creating JDBC connectionn");
// create statement
System.out.println("Create JDBC statement");
Statement statement = connection.createStatement();
System.out.println("Done creating JDBC statementn");
// query the data
System.out.println("Query demo");
ResultSet resultSet = statement.executeQuery("SELECT DATA_ID FROM TEMP_TABLE");
System.out.println("Metadata:");
System.out.println("================================");
// fetch metadata
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
System.out.println("Number of columns=" +
resultSetMetaData.getColumnCount());
statement.close();
}
private static Connection getConnection()
throws SQLException
{
try
{
Class.forName("net.snowflake.client.jdbc.SnowflakeDriver");
}
catch (ClassNotFoundException ex)
{
System.err.println("Driver not found");
}
// Class.forName("net.snowflake.client.jdbc.SnowflakeDriver");
// build connection properties
Properties properties = new Properties();
properties.put("user", "XXX"); // replace "" with your username
properties.put("password", "XXXX"); // replace "" with your password
properties.put("account", "XXXX"); // replace "" with your account name
properties.put("db", "db1"); // replace "" with target database name
properties.put("schema", "MYSCHEMA"); // replace "" with target schema name
//properties.put("tracing", "on");
// create a new connection
String connectStr = null;//System.getenv("SF_JDBC_CONNECT_STRING");
// use the default connection string if it is not set in environment
if(connectStr == null)
{
connectStr = "jdbc:snowflake:XXXX"; // replace accountName with your account name
}
return DriverManager.getConnection(connectStr, properties);
}
}
How to import/integrate this driver for java stored procedure?

When I try to insert values into my H2 Table it says table not found, even though the table exists What could be wrong?

I am using Netbeans to make a POS system and I am trying to insert values into an H2 table.The database connection seems to function properly and the table exists but I keep getting the table not found error. Here is my code to make the connection
package h2finalproj;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
/**
*
* #author Connor Hicks
*/
public class HDBConnect {
public static Connection connDB()
{
try {
Class.forName("org.h2.Driver");
Connection conn=DriverManager.getConnection("jdbc:h2:~\\cghicks1 [ database];","cghicks1","");
return conn;
} catch (ClassNotFoundException | SQLException e) {
JOptionPane.showMessageDialog(null, e);
}
return null;
}
}
public class H2FinalProj extends javax.swing.JFrame {
Connection connect = null;
ResultSet rst=null;
PreparedStatement pst=null;
PreparedStatement crt = null;
public H2FinalProj() {
initComponents();
connect = HDBConnect.connDB();
}
Here is my code to insert values:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
try {
String sql;
sql = "INSERT INTO \"PUBLIC\".INVENTORY (PRODNUM, PRODNAME, PRICE, STOCK) values(?,?,?,?)";
pst = connect.prepareStatement(sql);
pst.setString(1, prodNum2.getText());
pst.setString(2,prodName2.getText());
pst.setString(3, price2.getText());
pst.setString(4, numStock2.getText());
pst.execute();
JOptionPane.showMessageDialog(null, "Success !");
} catch (SQLException | HeadlessException e) {
JOptionPane.showMessageDialog(null, e);
}
}
And the error I am receiving looks something like this:
SQL Exception Table "Inventory not found" [42102-190]
I have found somewhat similar questions and solutions but nothing that seems to be close or clear enough to solve this problem.
This question has a problem description which is extremely similar to mine however both my application and console are already using 1.4, so the solution does not seem to help me.
Embedded h2 database: getting connection but table not found
How might I solve this?

java.sql.SQLSyntaxErrorException:: ORA-00907: missing right parenthesis error

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

java.lang.ClassNotFoundException Netbeans java derby

I use Netbeans, doing a java app. I created a class ConnectDB for db connetion using Java DB in netbeans. i started the server and ten conneted to db. when i run the file it produce
java.lang.ClassNotFoundException: org.apache.derby.jdbc.ClientDriver # 25 line
and
java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/Libraryprj;create=true
#30 th line of code
the code is below
package Lms;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
/**
*
* #author JOJO
*/
public class ConnectDB {
static Connection conn;
public static void main(String[] args) {
String driver = "org.apache.derby.jdbc.ClientDriver";
String connectionURL = "jdbc:derby://localhost:1527/Libraryprj;create=true";
String createString = "CREATE TABLE Employee (NAME VARCHAR(32) NOT NULL, ADDRESS VARCHAR(50) NOT NULL)";
try {
Class.forName(driver);
} catch (java.lang.ClassNotFoundException e) {
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(connectionURL);
Statement stmt = (Statement) conn.createStatement();
stmt.executeUpdate(createString);
PreparedStatement psInsert = conn.prepareStatement("insert into Employee values (?,?)");
psInsert.setString(1, args[0]);
psInsert.setString(2, args[1]);
psInsert.executeUpdate();
Statement stmt2 = (Statement) conn.createStatement();
ResultSet rs = stmt2.executeQuery("select * from Employee");
int num = 0;
while (rs.next()) {
System.out.println(++num + ": Name: " + rs.getString(1) + "\n Address" + rs.getString(2));
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
get this library
http://repo.maven.apache.org/maven2/org/apache/derby/derbyclient/10.9.1.0/derbyclient-10.9.1.0.jar
and copy it to Derby's libs folder.
If you use Tomcat download derbyclient.jar from here . And copy the jar file to Tomcat's lib folder.

JDBC connectivity with open office database

I am using openoffice database for my project....Please help me know how to connect to open office database using JDBC.
Below code is for JAVA. Transform to JSP accordingly.
import java.text.ParseException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main
{
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws ParseException
{
try
{
String db_file_name_prefix = "c:\\mydbdir\\mydb";
Connection con = null;
// Load the HSQL Database Engine JDBC driver
// hsqldb.jar should be in the class path or made part of the current jar
Class.forName("org.hsqldb.jdbcDriver");
// connect to the database. This will load the db files and start the
// database if it is not alread running.
// db_file_name_prefix is used to open or create files that hold the state
// of the db.
// It can contain directory names relative to the
// current working directory
con = DriverManager.getConnection("jdbc:hsqldb:file:" + db_file_name_prefix, // filenames
"sa", // username
""); // password
Statement statement = con.createStatement();
//look at " for table name
ResultSet rs = statement.executeQuery("SELECT * FROM \"User\"");
//print the result set
while (rs.next())
{
System.out.print("ID: " + rs.getString("ID"));
System.out.print(" first name: " + rs.getString("firstname"));
System.out.println(" last name: " + rs.getString("lastname"));
}
statement.close();
con.close();
} catch (SQLException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
ex.printStackTrace();
} catch (ClassNotFoundException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Never tried it but seems like OpenOffice uses HSQLDB database underneath in file mode. Looks like you can connect to this HSQLDB database directly: Java: Creating a JDBC Connection to OpenOffice.Org Databases.
See also:
How to connect to a "normal" (not embedded) HSQL database?

Categories