How to run SQL(MYSQL) stored procedure in JAVA jdbc? - java

I'm a MySQL user and I have been using following statements in MySQL Workbench :
(these statements are based on Select column names whose entries are not null)
SET group_concat_max_len = 4294967295;
SELECT GROUP_CONCAT(
' SELECT ',QUOTE(COLUMN_NAME),
' FROM ( select * from table_name where s3_01 = ', coloumn1,' ) abc',
' WHERE `',REPLACE(COLUMN_NAME, '`', '``'),'` IS NOT NULL',
' HAVING COUNT(*)'
SEPARATOR ' UNION ALL ')
INTO #sql
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'table_name';
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Although it work in my workbench, I do not know how to make it work in java.
for example, I made following code:
String sql1 = "SET group_concat_max_len = 4294967295;";
String sql2 = " SELECT GROUP_CONCAT(' SELECT ',QUOTE(COLUMN_NAME), ' FROM ( select * from ptc_weight where s3_01 = ',column1,' ) abc', ' WHERE `',REPLACE(COLUMN_NAME, '`', '``'),'` IS NOT NULL', ' HAVING COUNT(*)' SEPARATOR ' UNION ALL ') INTO #sql FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'ptc_weight'; ";
String sql3 = " PREPARE stmt FROM #sql; ";
String sql4 = " EXECUTE stmt;";
String sql5 = " DEALLOCATE PREPARE stmt;";
String[] result = getResult(sql1+sql2+sql3+sql4+sql5);
public static String[][] getResult(String sql) {
System.out.println(sql);
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String[][] resultTable = null;
try {
con = getCon();
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
ResultSetMetaData result = rs.getMetaData();
int rowNum=0;
// Go to the last row
rs.last();
rowNum = rs.getRow();
// Reset row before iterating to get data
rs.beforeFirst();
int colNum = result.getColumnCount();
resultTable = new String[rowNum][colNum];
for(int itr1=0; itr1<rowNum; itr1++){
rs.next();
for(int itr2=0; itr2<colNum; itr2++){
resultTable[itr1][itr2] = rs.getObject(itr2+1).toString();
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
dbclose(con, ps, rs);
}// finally
return resultTable;
}
However, it does not work. I guess I made a wrong code for utilizing stored procedure, but I don't have any idea to deal with this problem.

CallableStatement callableStatement = null;
String getDBUSERByUserIdSql = "{call getDBUSERByUserId(?,?,?,?)}";
callableStatement.setInt(1, 10);
callableStatement.registerOutParameter(2, java.sql.Types.VARCHAR);
callableStatement.registerOutParameter(3, java.sql.Types.VARCHAR);
callableStatement.registerOutParameter(4, java.sql.Types.DATE);
// execute getDBUSERByUserId store procedure
callableStatement.executeUpdate();
String userName = callableStatement.getString(2);
String createdBy = callableStatement.getString(3);
Date createdDate = callableStatement.getDate(4);
System.out.println("UserName : " + userName);
System.out.println("CreatedBy : " + createdBy);
System.out.println("CreatedDate : " + createdDate);
Here Is Full Example. You can modify your code as you need.
Simple one with less argument and with resultset :
CallableStatement cstmt = con.prepareCall("{call getEmployeeDetails(?, ?)}");
cstmt.setInt("employeeId", 123);
cstmt.setInt("companyId", 456);
ResultSet rs = cstmt.executeQuery();

Related

set value = 0 while updating record in jsp

im trying to update a column using jsp
here i want my count column value to be updated as zero
count = 0
how can i do that. through this code value is not updating
String code = request.getParameter("code");
Connection conn = null;
Statement st=null;
ResultSet rs = null;
PreparedStatement ps = null;
int count = 0;
try{
conn = DataBaseConnection.initializeDatabase();
String query1 = null;
conn.setAutoCommit(false);
query1 = "update employee set count = ? where code= ' +code+' ";
ps = conn.prepareStatement(query1);
ps.setInt(1, count);
ps.executeUpdate();
}
catch (Exception e) {
e.printStackTrace();
}
here is my record in db
employee table
CODE VARCHAR2(12)
COUNT NUMBER(3)
You can replace
"update employee set count = ? where code= ' +code+' ";
with
"update employee set count = ? where code= '" + code + "'";
e.g. if the value of code is xyz then after this change, the query will become:
"update employee set count = ? where code= 'xyz'";
However, I recommend you do it as follows to avoid the SQL Injection:
query1 = "update employee set count = ? where code= ?";
ps = conn.prepareStatement(query1);
ps.setInt(1, count);
ps.setString(2, code);

Operation not allowed after ResultSet closed (mysql, java)

I was stuck with the error , here my line number 42 is while(rs.next()){, please help me with this i am stuck at this for few hrs.
> Exception in thread "main" java.sql.SQLException: Operation not allowed after ResultSet closed
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:937)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:872)
at com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:740)
at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:6326)
at removeStopwords.RemoveStopwords.main(RemoveStopwords.java:42)
This is my code:
package removeStopwords;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.StringTokenizer;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class RemoveStopwords {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/mydbv2";
// Database credentials
static final String USER = "root";
static final String PASS = "***";
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = null;
Statement stmt = null;
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection(DB_URL, USER, PASS);
stmt = (Statement) conn.createStatement();
String sql;
ResultSet rs = null;
ResultSet rs2 = null;
ResultSet rs3 = null;
java.sql.PreparedStatement ps = null;
int event_id = 10;
sql = "SELECT id,text from tweet where event_id = " + event_id;
rs = stmt.executeQuery(sql);
String text = "";
Long id;
while (rs.next()) {
id = rs.getLong("id");
text = rs.getString("text");
System.out.println("tweet = " + text);
text = text.replaceAll("http[^\\s]+", "");
text = text.replaceAll("www[^\\s]+", "");
System.out.println("tweet after removal of links= " + text);
StringTokenizer st = new StringTokenizer(text);
while (st.hasMoreTokens()) {
String stopword = st.nextToken();
System.out.println("stopword : " + stopword);
sql = "SELECT * from stopwords WHERE word =" + '"'+stopword+'"';
rs2 = stmt.executeQuery(sql);
if (rs2.next()) {
text = text.replaceAll(stopword, "");
System.out.println("tweet after removing stopword = " + text);
}
sql = "SELECT * from filtertweet where tweet_id = " + id + "";
rs3 = stmt.executeQuery(sql);
if (!rs3.next()) {
sql = "INSERT INTO filtertweet VALUES(?,?)";
ps = conn.prepareStatement(sql);
ps.setLong(1, id);
ps.setString(2, text);
ps.executeUpdate();
}
}
}
stmt.close();
conn.close();
}
}
A Statement object can have only one active ResultSet, so when you execute rs2 = stmt.executeQuery(sql), the first ResultSet (rs) gets closed.
Create two Statement objects, one for rs and another for rs2.
Quoting the javadoc of Statement:
By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists.
One ResultSet for one Statement is valid. When you are executing multiple queries use various Statements.
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = null;
Statement stmt = null;
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection(DB_URL, USER, PASS);
stmt = (Statement) conn.createStatement();
String sql;
ResultSet rs = null;
ResultSet rs2 = null;
ResultSet rs3 = null;
java.sql.PreparedStatement ps = null;
int event_id = 10;
sql = "SELECT id,text from tweet where event_id = " + event_id;
rs = stmt.executeQuery(sql);
String text = "";
Long id;
while (rs.next()) {
id = rs.getLong("id");
text = rs.getString("text");
System.out.println("tweet = " + text);
text = text.replaceAll("http[^\\s]+", "");
text = text.replaceAll("www[^\\s]+", "");
System.out.println("tweet after removal of links= " + text);
StringTokenizer st = new StringTokenizer(text);
while (st.hasMoreTokens()) {
String stopword = st.nextToken();
System.out.println("stopword : " + stopword);
sql = "SELECT * from stopwords WHERE word =" + '"'+stopword+'"';
Statement stmt2 = conn.createStatement();
rs2 = stmt2.executeQuery(sql);
if (rs2.next()) {
text = text.replaceAll(stopword, "");
System.out.println("tweet after removing stopword = " + text);
}
sql = "SELECT * from filtertweet where tweet_id = " + id + "";
Statement stmt3 = conn.createStatement();
rs3 = stmt3.executeQuery(sql);
if (!rs3.next()) {
sql = "INSERT INTO filtertweet VALUES(?,?)";
ps = conn.prepareStatement(sql);
ps.setLong(1, id);
ps.setString(2, text);
ps.executeUpdate();
}
}
}
stmt.close();
conn.close();
}
JDBC does not allow you to close the Statement that created the ResultSet or to execute another query that creates a ResultSet using the same Statement. Create different Statement objects and Resultset objects, make sure to not use the same Statement objects to execute two different Resultset statements.

Passing array parameter in prepare statement - getting "java.sql.SQLFeatureNotSupportedException"

I am facing error java.sql.SQLFeatureNotSupportedException in my prepare statement. I am using Mysql database.
Below is my code.
class tmp {
public static void main(String arg[]) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/sample", "root", "root");
PreparedStatement pst = conn
.prepareStatement("select * from userinfo where firstname in(?)");
String[] Parameter = { "user1", "Administrator" };
Array sqlArray = conn.createArrayOf("VARCHAR", Parameter);
pst.setArray(1, sqlArray);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
For Mysql -
Setting array is not possible in Mysql.
Instead of that you can form a query for (?,?,..) in the loop and same way for setting values.
String[] Parameter = { "user1", "Administrator" };
String query = "select * from userinfo where firstname in (";
String temp = "";
for(i = 0; i < Parameter.length; i++) {
temp += ",?";
}
temp = temp.replaceFirst(",", "");
temp += ")";
query = query + temp;
PreparedStatement pst = conn.prepareStatement(query);
so query becomes
select * from userinfo where firstname in (?,?)
and pass values also using loop.
For Oracle -
ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor("CHAR_ARRAY", conn);
String[] Parameter = { "user1", "Administrator" };
java.sql.Array sqlArray = new oracle.sql.ARRAY(arrayDescriptor, conn, content);
.
.
pstmt.setArray(1, sqlArray);
Error message is very clear. And MySQL does not support custom data types.
Currently MySQL is supporting only:
Numeric Type
Date and Time Type
String Type
Or, you can use each of the input values as a set of values of IN function in MySQL.
Change your JAVA code as follows:
StringBuilder sbSql = new StringBuilder( 1024 );
sbSql.append( "select * from userinfo where firstname in(" );
for( int i=0; i < Parameter.length; i++ ) {
if( i > 0 ) sbSql.append( "," );
sbSql.append( " ?" );
} // for
sbSql.append( " )" );
PreparedStatement pst = conn.prepareStatement( sbSql.toString() );
for( int i=0; i < Parameter.length; i++ ) {
pst.setString( i+1, Parameter[ i ] );
} // for
ResultSet rs = pst.executeQuery();
Convert List to a comma separated String and use it.
Class Tmp {
public static void main(String arg[]) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/sample", "root", "root");
// Consider this list is already constructed
List<String> parameter = new ArrayList<String>();
parameter.add("user1");
parameter.add("Administrator");
String parameterStr = "'" + String.join("','", parameter) + "'";
PreparedStatement pst = conn.prepareStatement("select * from userinfo where firstname in(" + parameterStr + ")");
ResultSet rs = pst.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Inserting records into a MySQL table using Java

I created a database with one table in MySQL:
CREATE DATABASE iac_enrollment_system;
USE iac_enrollment_system;
CREATE TABLE course(
course_code CHAR(7),
course_desc VARCHAR(255) NOT NULL,
course_chair VARCHAR(255),
PRIMARY KEY(course_code)
);
I tried to insert a record using Java:
// STEP 1: Import required packages
import java.sql.*;
import java.util.*;
public class SQLInsert {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/iac_enrollment_system";
// Database credentials
static final String USER = "root";
static final String PASS = "1234";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
Scanner scn = new Scanner(System.in);
String course_code = null, course_desc = null, course_chair = null;
try {
// STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// STEP 3: Open a connection
System.out.print("\nConnecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println(" SUCCESS!\n");
// STEP 4: Ask for user input
System.out.print("Enter course code: ");
course_code = scn.nextLine();
System.out.print("Enter course description: ");
course_desc = scn.nextLine();
System.out.print("Enter course chair: ");
course_chair = scn.nextLine();
// STEP 5: Excute query
System.out.print("\nInserting records into table...");
stmt = conn.createStatement();
String sql = "INSERT INTO course " +
"VALUES (course_code, course_desc, course_chair)";
stmt.executeUpdate(sql);
System.out.println(" SUCCESS!\n");
} catch(SQLException se) {
se.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
if(stmt != null)
conn.close();
} catch(SQLException se) {
}
try {
if(conn != null)
conn.close();
} catch(SQLException se) {
se.printStackTrace();
}
}
System.out.println("Thank you for your patronage!");
}
}
The output appears to return successfully:
But when I select from MySQL, the inserted record is blank:
Why is it inserting a blank record?
no that cannot work(not with real data):
String sql = "INSERT INTO course " +
"VALUES (course_code, course_desc, course_chair)";
stmt.executeUpdate(sql);
change it to:
String sql = "INSERT INTO course (course_code, course_desc, course_chair)" +
"VALUES (?, ?, ?)";
Create a PreparedStatment with that sql and insert the values with index:
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, "Test");
preparedStatement.setString(2, "Test2");
preparedStatement.setString(3, "Test3");
preparedStatement.executeUpdate();
this can also be done like this if you don't want to use prepared statements.
String sql = "INSERT INTO course(course_code,course_desc,course_chair)"+"VALUES('"+course_code+"','"+course_desc+"','"+course_chair+"');"
Why it didnt insert value is because you were not providing values, but you were providing names of variables that you have used.
This should work for any table, instead of hard-coding the columns.
//Source details
String sourceUrl = "jdbc:oracle:thin:#//server:1521/db";
String sourceUserName = "src";
String sourcePassword = "***";
// Destination details
String destinationUserName = "dest";
String destinationPassword = "***";
String destinationUrl = "jdbc:mysql://server:3306/db";
Connection srcConnection = getSourceConnection(sourceUrl, sourceUserName, sourcePassword);
Connection destConnection = getDestinationConnection(destinationUrl, destinationUserName, destinationPassword);
PreparedStatement sourceStatement = srcConnection.prepareStatement("SELECT * FROM src_table ");
ResultSet rs = sourceStatement.executeQuery();
rs.setFetchSize(1000); // not needed
ResultSetMetaData meta = rs.getMetaData();
List<String> columns = new ArrayList<>();
for (int i = 1; i <= meta.getColumnCount(); i++)
columns.add(meta.getColumnName(i));
try (PreparedStatement destStatement = destConnection.prepareStatement(
"INSERT INTO dest_table ("
+ columns.stream().collect(Collectors.joining(", "))
+ ") VALUES ("
+ columns.stream().map(c -> "?").collect(Collectors.joining(", "))
+ ")"
)
)
{
int count = 0;
while (rs.next()) {
for (int i = 1; i <= meta.getColumnCount(); i++) {
destStatement.setObject(i, rs.getObject(i));
}
destStatement.addBatch();
count++;
}
destStatement.executeBatch(); // you will see all the rows in dest once this statement is executed
System.out.println("done " + count);
}
There is a mistake in your insert statement chage it to below and try :
String sql = "insert into table_name values ('" + Col1 +"','" + Col2 + "','" + Col3 + "')";

PrepareStatement error

I get an error in this function but I don't know why.
Can you help me?
The error is in the row where I call executeQuery()
public static int numeroElementi(String table) throws SQLException {
// viene reistanziata perché questa è una funziona statica!
String DB_URL = "jdbc:mysql://localhost:3306/kmzero";
Connection connection = DriverManager.getConnection(DB_URL, "root", "root");
String query = "SELECT COUNT(*) AS count FROM ? ";
PreparedStatement pStatement = connection.prepareStatement(query);
pStatement.setString(1, table);
try {
ResultSet resultSet = pStatement.executeQuery();
try {
if (resultSet.next())
return resultSet.getInt("count");
else
return 0;
} finally {
resultSet.close();
}
} finally {
pStatement.close();
}
}
I think you cannot pass the tablename (also columnNames) as a parameter and this is the time when tableName should be concatenated in your string.
String query = "SELECT COUNT(*) AS count FROM " + table;
if your table needs to be escape,
String query = "SELECT COUNT(*) AS count FROM `" + table + "`";

Categories

Resources