I am Using the following code :
DefaultListModel dModel = (DefaultListModel)jList1.getModel();
txtBilln.enableInputMethods(false);
try{
Class.forName("com.mysql.jdbc.Drvier");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/jns","root","jns");
Statement stmt = null ;
ResultSet rs = null ;
String SQL = "SELECT * FROM ELEB";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
while (rs.next()){
String bno= rs.getString("billn");
String cnamme = rs.getString("cname");
dModel.addElement(bno + "-" + cnamme);
}
jList1.setModel(dModel);
con.close();
}catch(Exception e){
JOptionPane.showMessageDialog(this, e.getMessage());
e.printStackTrace();
}
Related
tu.java
//jdbc connection
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/st","root","root");
//query
String query = "select indexno,lname,fname from reg where indexno= ? and tel=?";
PreparedStatement ps = con.prepareStatement(query);
ps.setInt(1, Integer.parseInt(in.getText()));
ps.setInt(2, Integer.parseInt(tl.getText()));
ResultSet rs = ps.executeQuery();
if(rs.next()) {
String name11 =rs.getString("lname");
String name1 =rs.getString("fname");
int indexno11 =rs.getInt("indexno");
//passing value to wt frame
wt wt = new wt(name11, name1, indexno11);
wt.show();
}
rs.close();
con.close();`
wt.java
loging page index number used to check next frame credentials
public void actionPerformed(ActionEvent e) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/st","root","root");
String query = "select age from reg where indexno=indexno11";
PreparedStatement ps = con.prepareStatement(query);
ResultSet rs = ps.executeQuery();
if(rs.next()) {
String age11 =rs.getString("age");
System.out.println(age11);
}
rs.close();
con.close();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
You forgot your literalising single quotes around the value in the String query line.
String query = "select age from reg where indexno='indexno11'";
I would like to know how I can return a result set from a query as a map.
This is my query where 'nameCodesString' is a list of strings e.g. ('raul', 'peter', 'shawn'):
try (PreparedStatement stmt = conn.prepareStatement("select n.CODE, l.VALUE"
+ " from TNAME n join TPROPERTIES l on n.UIDPK = l.OBJECT_UID"
+ " where n.CODE IN (" + nameCodesString + ")")) {
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
log.info("rs {}",rs);
nameCode = rs.getString(1);
displayName = rs.getString(2);
Person.add(new PersonDTO(nameCode, displayName, ""));
}
}
}
The result should be a code and a value. I am not sure how I can do this all in one connection to the database.
Hello that is my suggestion
String req="select n.CODE, l.VALUE from TNAME n join"
+" TPROPERTIES l on n.UIDPK = l.OBJECT_UID "
+" where n.CODE IN ?";
PreparedStatement stmt=null;
ResultSet rs=null;
try{
stmt=conn.prepareStatement(req);
stmt.setString(1, nameCodesString);
rs=stmt.executeQuery();
while(rs.next()){
nameCode = rs.getString(1);
displayName = rs.getString(2);
Person.add(new PersonDTO(nameCode, displayName, ""));
}
}
catch(SQLException ex){
System.out.println(ex);
}
finally{
if(rs!=null){
try{
rs.close();
}
catch(SQLException ex){}
}
if(stmt!=null){
try{
stmt.close();
}
catch(SQLException ex){}
}
if(conn!=null){
try{
conn.close();
}
catch(SQLException ex){}
}
}
It is just my suggestion i hope it can be a tip for you!
Best regards...
I failed to understand how preparedStatements should be used. In the end I decided not to use them and stick to executing a query string:
Map<String, String> personDetails = new Hashmap();
try (Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select n.CODE, l.VALUE"
+ " from TNAME n join TPROPERTIES l on n.UIDPK = l.OBJECT_UID"
+ " where n.CODE IN (" + nameCodesString + ")")) {
while (rs.next()) {
nameCode = rs.getString(1);
displayName = rs.getString(2);
personDetails.put(nameCode, displayName);
}
}
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.
I have following code,
private void trans_tabMouseClicked(java.awt.event.MouseEvent evt) {
try{
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/online_store","poornima","mit1234");
if(con != null){
String query = "SELECT * FROM bill";
rs = stmt.executeQuery(query);
System.out.println("fffgg");
ResultSetMetaData rsmt = rs.getMetaData();
int c = rsmt.getColumnCount();
Vector row = new Vector();
DefaultTableModel model = (DefaultTableModel)expense_table.getModel();
while(rs.next())
{
row = new Vector(c);
for(int i = 1; i <= c; i++)
{
row.add(rs.getString(i));
}
model.addRow( row );
}
}
}catch(Exception ex){
System.out.println(ex);
}
}
in the above code query is not executed. Fired null pointer exception at rs = stmt.executeQuery(query); line. I tried hours, but I can't figure out where the issue is. Please help.
because stmt is null.
use this
String query = "SELECT * FROM bill";
stmt = con.createStatement();
rs = stmt.executeQuery(query);
instead of this
String query = "SELECT * FROM bill";
rs = stmt.executeQuery(query);
refer this
I need to open a div tag from jsp if the value of the resultset is more than 30000. But how to store the value of rs as an int?
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
con = DriverManager.getConnection (urlPrefix, "uname", "pwd");
System.out.println("got connection");
stmt = con.createStatement();
String strQuery = "SELECT count(*) FROM tablename where condition stmt";
rs = stmt.executeQuery(strQuery);
if (rs>30000) {
request.getParameter("view");
}
System.out.println("executed query");
} catch(Exception e) {
System.out.println(e);
}
rs = stmt.executeQuery(strQuery);
stmt.executeQuery return a ResultSet object and from the object we can retrive the result of
the query like below for your case.
rs = stmt.executeQuery(strQuery);
int count=0;
if(rs.next()){
count=rs.getInt(1);
}