get maximum value from database and set into jtextfield [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to get maximum value from database (bill_number) and set into set into jtextfield. but I get error (void type not allowed here).
where is problem in my code
public void number(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn = DriverManager.getConnection("Jdbc:Odbc:ds_comboPractice");
String sql = "select count(Bill_Number)=? from combopractice";
PreparedStatement pst = cn.prepareStatement(sql);
pst.setString(1, txt4.setText(sql));
pst.executeQuery();
cn.close();
}
catch(ClassNotFoundException ex){
JOptionPane.showMessageDialog(this, "Class not found");
}
}

You need to do txt4.getText not setText
You then want to assign the results of your executeQuery to a ResultSet and read the result from that.

pst.setString(1, txt4.setText(sql));
setText() returns void
You need a String argument

Related

What is the reason for this query is not working? Got: MySQLSyntaxErrorException Unknown column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
My columns at customer table
cusid cusname cusadress paidamount email
cus123 damidu kegalle 45 adfff
This is my Java query
String query = "UPDATE `customer` SET `cusname`='"+jTextField_name.getText()+"',`cusadress`='"+jTextField_adress.getText()+"',`paidamount`='"+jTextField_amount.getText()+"',`email`="+jTextField_emailuser.getText()+" WHERE `cusid` = "+jTextField_id.getText();
executeSQlQuery(query, "Updated");
Error
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'cus123' in 'where clause'
What is the reason for this update is not working , but insert and delete statement works?
Use PreparedStatement like this below :
String updateQuery = "UPDATE customer SET cusname=?,cusadress=?,paidamount=?,email=? WHERE cusid=?";
PreparedStatement stmt=con.prepareStatement(updateQuery); // con is reference variable of Connection class
stmt.setString(1, jTextField_name.getText());
stmt.setString(2, jTextField_adress.getText());
stmt.setInt(3, jTextField_amount.getText());
stmt.setString(4, jTextField_emailuser.getText());
stmt.setString(5, jTextField_id.getText());
Hope this helps.

Sql to java JDBC database [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a problem with searching a particular contact using part of a name. I know how it would look like in SQL but I cant implement it using Java.
if (rs.getString(nameTable LIKE '%name1%';)
Consider adding the LIKE clause to your SQL query instead of handling it in java code:
try(PreparedStatment ps = con.prepareStatement("SELECT * " +
" FROM Contact WHERE contactName like ?")) {
ps.setString(1, "%name1%");
try(ResultSet rs = ps.executeQuery()) {
while(rs.next()) {
//process your data
}
}
} catch(Exception e) {
//deal with it
}

insert jdatechooser from java to database [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How can I insert jdatechooser from java to database? I have used all the solutions of the net but still I can't insert. Please help me.
I'm using eclipse environment.
This is my code:
try {
PreparedStatement stm= (PreparedStatement) con.prepareStatement("INSERT INTO d"+
"(dateEntré)"+
"VALUES(?)");
((PreparedStatement)stm).setDate(1,convertUtilDateToSqlDate(dateChooser.getDate()));
statement.execute();
JOptionPane.showMessageDialog(null,"added");
} catch (Exception e1) {
JOptionPane.showMessageDialog(null,e1.getMessage());
}
Your code example seems to be wrong, and this could just be a typo, but since you've provided no other information with regards to the error, this is all we have to go on...
You create a PreapredStatement called stmt, but then use statement to execute the query.
You should be using the same variable/instance to bind and excute the query. You should probably also use executeUpdate instead of execute, this provides some additional information when the statement is executed about the number of rows that were affected by the update.
try {
PreparedStatement stm= (PreparedStatement) con.prepareStatement("INSERT INTO d (dateEntré) VALUES(?)");
stm.setDate(1,convertUtilDateToSqlDate(dateChooser.getDate()));
int rows = statement.executeUpdate();
if (rows > 0) { // Should be 1
JOptionPane.showMessageDialog(null,"added");
} else {
JOptionPane.showMessageDialog(null,"Update failed for unknown reason");
}
} catch (Exception e1) {
JOptionPane.showMessageDialog(null,e1.getMessage());
e1.printStackTrace();
}
This of course assumes that the database column is a compatible type of java.sql.Date
Take a closer look at JDBC Database Access and Using Prepared Statements for more details.

JDBC is not executing SHOW DATABASES command [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
i want to get a list of databases stored in mysql and put in java table using command "show databases" via a resultset. but its not working.
DefaultTableModel model=(DefaultTableModel)dbTbl.getModel();
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql//localhost/:3306","root","password");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("show databases;");
while(rs.next())
{
String db=rs.getString(1);
model.addRow(new Object[] {db});
}
rs.close();
stmt.close();
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"nahi chalda");
}
That's not the best way to get a list of Databases in JDBC. Here's the way it's done - using MetaData
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/:3306","root","password");
DatabaseMetaData meta = con.getMetaData();
ResultSet resultSet = meta.getCatalogs();
while (resultSet.next()) {
String db = resultSet.getString("TABLE_CAT");
model.addRow(new Object[] {db});
}
resultSet.close();
con.close();
See also: how to get list of Databases "Schema" names of MySql using java JDBC
I just forgot to add colon after "jdbc:mysql
Correct code is:
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/:3306","root","password");
It works ,......... !!!

create jdbc connection and retrieve the first values of the first 4 columns [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need your help in creating a java bean to retrieve the first 4 columns in a table called "m_connection". I want to retrieve the first 4 columns and store them in variables.
They are all string.
this code sample may help you:
String sql="select one,two,three,four from table where id=1";
Connection con = DriverManager.getConnection("");//get connection here
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
String[] result = new String[4];
while(rs.next()){
result[0] = rs.getString(1);
result[1] = rs.getString(2);
result[2] = rs.getString(3);
result[3] = rs.getString(4);
}
First of all you need to establish jdbc connection then think for the data retrival
for jdbc connection you need to follow
Load the JDBC driver.
Define the connection URL.
Establish the connection.
Create a statement object.
Execute a query or update.
Process the results.
Close the connection.
refer details

Categories