I have a requirement to insert clob data in SQLServer Table.
below Code is throwing java.lang.AbstractMethodError: net.sourceforge.jtds.jdbc.ConnectionJDBC3.createClob()Ljava/sql/Clob
public void executeQueryWithPreparedStatements(int messageID,String xmlMessage,String xmlDataStorageType) throws SQLException {
String dataTypeSpecificSQL = "Insert into OXI_MESSAGE_AS_CLOB(messageid,MSG_content) values (?,?)";
PreparedStatement statement = null;
try {
statement = connection.prepareStatement(dataTypeSpecificSQL);
statement.setInt(1, messageID);
Clob clobData = connection.createClob();
clobData.setString(0,xmlMessage);
statement.setClob(2, clobData);
statement.executeUpdate();
}catch(SQLException sqle){
throw sqle;
}finally {
releaseResource(statement);
}
}
I am using Jtds-1.2.8.jar
When I checked the sourcecode I have found that createClob() method is not available in net.sourceforge.jtds.jdbc.ConnectionJDBC3 class
Whether someOne else also faced this issue ?
Please provide suggestion for how to insert clob data with Jtds Driver in SQLServer with this issue?
Related
I have a problem with a Dynamic Web Project, I've been with this error for a couple of weeks and I can not solve it, it's about jdbc driver.
public class DatosDAO {
private String url = "jdbc:mysql://localhost:3306/BCopia";
private String usuario = "root";
private String password = "";
public DatosDAO() {}
public boolean alta(Datos d) {
try {
Connection con = DriverManager.getConnection(url,usuario,password);
PreparedStatement ps = con.prepareStatement("INSERT INTO datos VALUES (null, ?, ?, ?, ?)");
ps.setString(1, d.getNombre());
ps.setString(2, d.getDirectorioOrigen());
ps.setString(3, d.getDirectorioDestino());
ps.setInt(4, d.getIntervaloDias());
ps.executeUpdate();
con.close();
} catch (Exception ex) {ex.printStackTrace();return false;}
return true;
}}
When attempt to run this method or any other method. I get the following error.
java.sql.SQLException: No suitable driver found for
jdbc:mysql://localhost:3306/BCopia at
java.sql.DriverManager.getConnection(Unknown Source) at
java.sql.DriverManager.getConnection(Unknown Source) at
modelo.DatosDAO.baja(DatosDAO.java:44) at
control.Securalia.baja(Securalia.java:43) etc
I have the driver in lib and have the driver called in the build path and everything else, it is a dynamic web project. Can someone tell me what happens? The DB exists, etc.
First of all you have to add the database driver to the project library. after that you should mention the driver class name here's an example with firebird driver :
Class.forName("org.firebirdsql.jdbc.FBDriver");
Check your mysql jdbc driver to find the class name
next you can establish a connection to you're database exactly like you've done with a connection URL and call the driver manager, here's an example :
String connectionURL = "jdbc:firebirdsql://127.0.0.1:3030/c:\\db.fdb";
Connection conn = DriverManager.getConnection(connectionURL, userName,password);
good luck !
ConnectionClass:
Here I used stm.execute but I'm not getting an error. I also used executeUpdate but I'm also getting an error.
package com.company;
import java.sql.*;
class ConnectionClass {
private static Connection con = null;
private static Statement stm = null;
private static ConnectionClass connectionClass;
public void createConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "root");
}
catch (Exception e) {
e.printStackTrace();
}
}
public void createTable() {
String Table_Name = "BOOK";
try {
stm = con.createStatement();
DatabaseMetaData dbm = con.getMetaData();
ResultSet rs = dbm.getTables(null, null, Table_Name, null);
if (rs.next()) {
System.out.println("Table" + Table_Name + "Already created");
} else {
String sql = "CREATE TABLE" + Table_Name + "(ID VARCHAR(200), title VARCHAR(200),author varchar(100),publisher varchar(100)";
stm.executeLargeUpdate(sql);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public ConnectionClass() {
createConnection();
createTable();
}
}
MainClass:
Here I think the main class is working properly.
package com.company;
public class Main {
public static void main(String[] args) throws Exception {
ConnectionClass connectionClass = new ConnectionClass();
}
}
ERROR:
The method executeLargeUpdate was added in Java 8 / JDBC 4.2 and judging by the error it has not been implemented in the MySQL Connector/J version you are using.
The solution is simple: don't use any of the Large methods in the API, and instead use executeUpdate, or - better in the case of DML - execute. Alternatively, update to a newer version of MySQL Connector/J, as executeLargeUpdate is implemented in newer versions of MySQL Connector/J (eg 5.1.46, but at least 5.1.37 (the version that added support)).
You will also need to address the syntax error pointed out by Adil.
You have a syntax error in your query. You current SQL Query:
String sql = "CREATE TABLE" + Table_Name + "(ID VARCHAR(200), title VARCHAR(200),author varchar(100),publisher(100)";
Corrected SQL Query:
String sql = "CREATE TABLE" + Table_Name + "(ID VARCHAR(200), title VARCHAR(200),author varchar(100),publisher varchar(100)";
(You are asking mysql to create a column publisher, but where's the datatype? Simply specify the datatype and it will work.)
EDIT:
So, finally wrapping up this question, let's have a look at what Ora Docs have to say about this:
executeLargeUpdate:
default long executeLargeUpdate(String sql) throws SQLException
Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.
This method should be used when the returned row count may exceed Integer.MAX_VALUE - A constant holding the maximum value an int can have, 2^31-1.
Note:This method cannot be called on a PreparedStatement or CallableStatement.
The default implementation will throw UnsupportedOperationException. This method is introduced since JDK 1.8
Also note the above point as stated in the docs. The default implementation is to throw an UnsupportedOperationException. This means that different JDBC drivers can have different implementations of Statement class. They can either implement it or leave it unimplemented, and if you invoke the method in 2nd case, the method will throw an UnsupportedOperationException, as stated in the docs.
By checking the oracle docs for this method, you can get more information about it. So the possibility could be that the driver version you are using is not supporting this method, so please update tot eh latest version of this driver and try it out.
I'm writing a small program with a mysql connection. I have to insert data into the database. The connection is ok but when I try to execute a query it doesn't work. all statements after executeQuery() statement doesn't work.
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
Connection connect=null;
PreparedStatement preparedStatement;
String JDBC_DRIVER="com.mysql.jdbc.Driver";
String DB_URL="jdbc:mysql//localhost/jarvis";
String USER = "test";
String PASS = "test";
try{
Class.forName(JDBC_DRIVER);
connect = DriverManager.getConnection(DB_URL, USER, PASS);
}
catch(ClassNotFoundException e){
out.println("Errore: "+e);
} catch (SQLException ex) {
Logger.getLogger(Prova.class.getName()).log(Level.SEVERE, null, ex);
}
out.println();
out.println("qui");
String query = "INSERT INTO users(Nome, Cognome, Username, Password) values(?, ?, ?, ?)";
try (PreparedStatement insert = connect.prepareStatement(query)) {
insert.setString(1, "name");
insert.setString(2, "sur");
insert.setString(3, "gvhgv");
insert.setString(4, "qfwe");
insert.executeUpdate();
}
catch(Exception e){
out.println("Errore "+e);
}
out.println("Fine");
}
on executeQuery it stop working and doesn't insert the values into the database.
P.S.: sorry for my English
foto
Firstly your JDBC Connection URL is wrong, it should be this
String DB_URL = jdbc:mysql://localhost/jarvis
instead of this:
String DB_URL = jdbc:mysql//localhost/jarvis
You can follow the MySQL documentation whenever you are in doubt about anything.
Please note that mentioning port is not necessary if it is skipped it would default to MySQL's default port 3306.
Secondly, use executeUpdate() method instead of executeQuery method. It is best suggested to test your code in chunks, like DB Connection is successful, able to retrieve data from the underlying DB and then inserting into the DB.
Or even a better way is to debug your code and at least provide where you find the NullPointerException as you say you're getting now!
Edit:
You must have your JDBC Connector/J jar available for your code to access it if you use an IDE (then in its build path) and in the CLASSPATH global variable (for references when we don't use an IDE). It is better to have it on your CLASSPATH if you are not going to change these dependencies any often.
Hope this helps!
Below is my Java program. I am calling a PLSQL procedure to update the Employee name. I turned off the commit in the PLSQL code so that I can do the commit and rollback from Java code. But even after I turned off the auto commit and doing explicit rollback, still the details are updated in the table.
How? I have no idea, please help.
Here's my Java code. In PLSQL, it just read the value and does an update statement . No commits.
public class TestCommit {
public static void main(String[] args) throws SQLException, IOException {
CallableStatement callableStatement = null;
Connection conn = null;
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager
.getConnection("jdbc:oracle:thin:testuser/testpwd#//testdb:1521/testbx");
conn.setAutoCommit(false);
String sql = "{call testpkg.saveemployee(?,?)}";
callableStatement = conn.prepareCall(sql);
callableStatement.setString("name", "spider");
callableStatement.setString("id", "A101");
callableStatement.executeQuery();
conn.rollback();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
// Close the statement
callableStatement.close();
// Close the connection
conn.close();
}
}
}
edit: PLSQL
CREATE OR REPLACE PROCEDURE saveemployee(
name IN employee.ename%TYPE,
id IN employee.eid%TYPE)
IS
BEGIN
UPDATE employee SET ename = name WHERE eid = id;
END;
My bad, I was calling a wrong procedure, there were two versions of the same procedure in two different packages,
one has the commit , the other one doesn't have the commit. I was calling the one that had the commit.
Now that the commit is removed from both procedures, my code seems to work now.
I have a postgres database and a table named "state_master". I'm simply fetching the data from this table by the following code.
import java.sql.*;
public class Test1
{
public static void main(String... s1)
{
try{
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432:secc_db","postgres", password");
Statement s = con.createStatement();
ResultSet rs = s.executeQuery("Select * from state_master");
}catch(SQLException e){ System.out.println(e);}
catch(Exception i){System.out.println(i);}
}
}
All I get is an error: relation state_master does not exist. Please help me to sort out this problem.
The erroe means the table state_master is not exist in the user(Database) postgres. Check whether you have created the table state_master in your database or not. If not first create the table then try to execute the program.
Your connect url is not correct. you are using colon(:) for specifying the database.
It should be a slash(/) like : "jdbc:postgresql://localhost:5432/secc_db"