如何使用Net Bean从Mysql插入和选择数据?

问题描述:

亲爱的,



i已经为登录表单创建了一个代码,我手动输入了mysql数据库的详细信息,现在我想以我的形式访问它。 ..

我也想插入数据。



这是我的代码,



当我执行它时,它将建立一个消息连接。

并直接跳转到错误消息并说在开始结果集之前。



Dear all,

i have made a code for login form and i have input details to mysql database manually, and now i want to access it in my form...
also i want to insert data as well.

this is my code,

and when i execute it, it will gives a message connection established.
and directly jump to error message and says "Before start of result set".

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

/**
 *
 * @author businesssupport
 */
public class VisaDocs {

    /**
     * @param args the command line arguments
     */
public static void main(String[] args) {
// TODO code application logic here
Connection conx = null;
Statement stmt = null;
ResultSet rs = null;
//String Driver="";
String URL = "jdbc:mysql://localhost:3306/entry";
String db ="entry";
String u_name = "root";
String u_pwd="";
String query ="select * from users";
//String u_1name, u_2name;

try{       
//Class.forName("com.mysql.jdbc.Driver").newInstance();
conx = DriverManager.getConnection(URL,u_name,u_pwd);
if(conx!= null){
    JOptionPane.showMessageDialog(null,"Connection has established","I N F O",JOptionPane.INFORMATION_MESSAGE);
stmt = conx.prepareStatement(query);
rs = stmt.executeQuery(query);


     String u_1name = rs.getString("username");
     String u_2name = rs.getString("password");

JOptionPane.showMessageDialog(null,u_1name,"This is supposed to be the MessageBox title.",JOptionPane.INFORMATION_MESSAGE);
}}


catch(Exception ex){
JOptionPane.showMessageDialog(null,ex.getMessage(),"E R R O R",JOptionPane.ERROR_MESSAGE);
}
 finally{
    try{
    conx.close();
    }catch(Exception ex){
    JOptionPane.showMessageDialog(null,ex.getMessage(),"closing err",JOptionPane.ERROR_MESSAGE);
    }
}  





我如何在消息框中显示所选数据。

ex :( null,'+ u_1name +'+ u_2name +',这应该是)

就像这样或任何其他方式....



also how do i show selected data in a message box.
ex : (null,'"+u_1name+"'"+u_2name+"',"This is supposed")
is it like this or any other way....

您需要通过调用 rs.next() [ ^ ]。
You need to move the result set cursor to the first row, by calling rs.next()[^].