it seems my code is not working. I am trying to extract data from microsoft NAV Cronus DB, but it appears JAVA can't find the table to print.
So I have 2 classes - DAL & Controller
DAL:
import java.sql.*;
public class DAL {
private static String connStr = "jdbc:sqlserver://Localhost;Databases=CronusDB;user=root;password=root;";
public static Connection getConn() throws SQLException {
return DriverManager.getConnection(connStr);
}
public static ResultSet findEmployeeData() throws SQLException {
Statement stmt;
stmt = getConn().createStatement();
String sqlStr = "select [No_], [First Name], [Last Name], [Initials], [Job Title] from [CRONUS Sverige AB$Employee];";
ResultSet rset = stmt.executeQuery(sqlStr);
return rset;
}
}
Connector:
import java.sql.*;
import t3.isprojekt.uppg2.dal.DAL;
public class Controller {
public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
DAL.getConn();
System.out.println("Connection Success!");
System.out.println(DAL.findEmployeeData());
}
}
When trying to execute the code I recieve the following error:
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'CRONUS Sverige AB$Employee'.
The problem is that the table does exist, and I have tried putting .dbo in front without success. Any ideas?
It seems like the database is very sensitive to upper- & lowercase letters. The tables are listed as "CRONUS Sverige..." in MSSQLServer, but when copying the name directly into JAVA it is pasted as "CRONUS SVERIGE" (uppercase). So finally got this working!
Related
In JDBC, I want to insert data using input from the keyboard.
As a result of finding the method, I came to know the scanner and wrote the code as follows.
package DB;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Scanner;
class insert{
Connection con;
Statement stmt;
Scanner scan = new Scanner(Syetem.in);
public insert() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:#localhost:1521:orcl";
con = DriverManager.getConnection(url,"scott","1234");
stmt = con.createStatement();
} catch(ClassNotFoundException ce) {
System.out.println(ce.getMessage());
} catch(SQLException se){
System.out.println(se.getMessage());
}
}
public void disconnect() throws SQLException{
if (stmt!=null) stmt.close();
if (con!=null) con.close();
}
public void insert() throws SQLException{
System.out.println("name:")
String employee_name=scan.next();
System.out.println("domain:")
String street=scan.next();
System.out.println("country:")
String city=scan.next();
String sql="insert into information values('"+name+"', '"+domain+"', '"+country+"')";
int n=stmt.executeUpdate(sql);
}
}
But it didn't run and got an error ...
The default method cannot be found in the class. Define a default method in the following format. public static void main (String [] args)
Where should I put the main function to fix the error?
What is the problem? The name of the table to be inserted is 'information'.
Any help would be greatly appreciated
Because JDBC is not yet familiar, if possible, if you write a full query including a connection as in the above query, I would be very grateful.
*my oracle version is 11g
For your code to run you need to provide a method that works as an entry point for your program, that method has the signature public static void main(String[] args) besides that, your class should be named Insert with capital I first because thats de standard y second because you have a method called insert, you will need to have something like this:
public class Insert {
public Insert() {
...
}
public void disconnect() throws SQLException {
...
}
public void insert() throws SQLException {
...
}
public static void main(String[] args) {
new Insert().insert();
}
}
I am a little bit confused,
I am trying to insert multiple rows to MS Access database from a java program using ucanaccess Java library.
I don't understand why the above (check title) SQL Exception is thrown when calling the 2nd insertRow() method?
The Exception is NOT thrown either by calling con.setAutoCommit(false); & con.commit(); methods or by re-executing the SQL query using the command rs = st.executeQuery(sql);. I also do not understand why the problem is solved by doing one of the above. What changes?
Thanks in advance.
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class db1 {
private Connection con;
protected Statement st;
protected ResultSet rs;
public db1() {
connect();
}
public void connect() {
try {
String driver = "net.ucanaccess.jdbc.UcanaccessDriver";
Class.forName(driver);
String db = "jdbc:odbc:Database1";
con = DriverManager.getConnection
("jdbc:ucanaccess://C:\\Users\\Κώστας\\Desktop\\Database1.accdb");
st = con.createStatement
(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE,
ResultSet.HOLD_CURSORS_OVER_COMMIT)
// con.setAutoCommit(false);
String sql = "select * from TableA";
rs = st.executeQuery(sql);
rs.insertRow();
// rs = st.executeQuery(sql);
rs.insertRow(); // HERE the SQL Exception is thrown.
// con.commit();
}
catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) {
new db1();
}
}
UCanAccess has some known issues with updatable ResultSets because it uses triggers on the HSQLDB backing tables to push the changes to the Access database file. A side effect of those triggers is that they can leave the HSQLDB ResultSet in an invalid state.
The problem you are experiencing may not manifest itself with con.setAutoCommit(false); because the triggers probably don't flush the changes to the Access database until the JDBC transaction is committed.
My code for connection with access database is this...its working fine here... i have tried to connect my database with java derby embedded database but always getting sql exception assuming the same table what changes do i need to connect my application with java derby embedded database??
package database;
import java.sql.*;
import javax.swing.JOptionPane;
public class database {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
try
{
String url = "jdbc:odbc:personnew";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(url);
Statement st=con.createStatement();
String sql="SELECT * FROM Person";
ResultSet rs=st.executeQuery(sql);
while(rs.next()){
String id=rs.getString("id");
String name=rs.getString("name");
String fathername=rs.getString("fathername");
JOptionPane.showMessageDialog(null,id+"\t"+name+"\t"+fathername);
}
// TODO code application logic here
}catch(Exception sqlEx){
System.out.println("Sql exception");
}
}
}
For one thing, You would need to use the correct JDBC driver; org.apache.derby.jdbc.EmbeddedDriver
http://db.apache.org/derby/papers/DerbyTut/embedded_intro.html
The tutorial in general is probably where you want to start as it tells you everything you need to know:
http://db.apache.org/derby/papers/DerbyTut/index.html
I have written a connection code with oracle. But still I am getting errors. I'll type the code of mine here.
import java.sql.*;
public class SimpleOraJava {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
// TODO Auto-generated method stub
DriverManager.registerDriver(new Oracle.jdbc.driver.OracleDriver());
String serverName="10.20.228.67";
String user="root";
String password="root";
String SID="abc";
String URL="jdbc:oracle:thin:#"+serverName+":"+1520+":"+SID;
Connection conn=DriverManager.getConnection(URL, user, password);
String SQL="Select employeename from employee";
Statement stat=conn.createStatement();
ResultSet rs=stat.executeQuery(SQL);
while (rs.next()){
System.out.println(rs.getInt(1));
}
stat.close();
conn.close();
}
}
It shows error in this line:
DriverManager.registerDriver(new Oracle.jdbc.driver.OracleDriver());
The error is on the word Oracle. It is asking me to create class in package oracle.jdbc.driver
Please somebody help!
Okay, assuming that class-paths are set up, and the appropriate .jar files are in the correct directories, the first thing that jumps out is I believe you need to import the package into your class. There should be a import oracle.jdbc.driver.*; line under the import java.sql.*; line also the DriverManager call should be
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
with the lowercase o, it's capitalized in your code.
Another thing might be, the version of the Oracle JDBC, and Oracle client you're using. According to this OTN Discussion post Oracle JDBC 10.2 is the last release to support the package oracle.jdbc.driver.
So basically according to the metalink page if you're using a JDBC 10.2 or older client, something like this will work:
import java.sql.*;
import oracle.jdbc.driver.*;
public class myjdbcapp
{
public static void main(String[] args) throws SQLException
{
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
String url = "jdbc:oracle:thin:#server:port:orcl";
String userName = "scott";
String password = "tiger";
Connection conn = DriverManager.getConnection (url, userName, password);
OracleCallableStatement myprocst = (OracleCallableStatement)
conn.prepareCall ("begin myproc(?); end;");
// ...
}
}
Clients newer than JDBC 10.2 will need to change import oracle.jdbc.driver.; to import oracle.jdbc.;
DriverManager.registerDriver(new Oracle.jdbc.driver.OracleDriver());
The package is oracle.jdbc.driver with a lowercase o.
package database;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import database.Dbconnect;
public class CreateQuery {
Connection conn;
public CreateQuery() throws ClassNotFoundException, SQLException, IOException {
conn=new Dbconnect().returnDatabaseConnection();
}
public int addNewLayertoDB(String feature_name,String shape,int Latitude , int Longitude , int feature_geom , String feature_details){
try {
PreparedStatement statement = null;
String table_name = feature_name + "_" + shape;
String query = "CREATE TABLE EtherMap "+table_name+" ("+ feature_name+" (20))";
statement = conn.prepareStatement(query);
statement.setString(1, feature_name);
statement.execute();
String squery = "ALTER TABLE EtherMap" +table_name+" ADD COLUMN geom int , ADD COLUMN shape character(10)";
return 1;
} catch (SQLException ex) {
return 0;
}
}
public void closeConn() throws SQLException {
if (conn != null) {
this.conn.close();
}
}
}
I have coded this createquery.java code which would create a table in postgres . I need to call it when I draw anything on a open layers map , using javascript in a jsp page . How do I call it ? Do I have to use the beans ?
It would be good design if you call it from servlet and servlet from jsp taking user input
See Also
Servlet
Make a request to your server, on a specific URL, probably using JavaScript
Map that URL to a specific Servlet
Code your servlet to execute whatever method, you like, of your class
Simple, isn't it?