问题是在st = con.createStatement()时发生NullPointException; rs = st.executeQuery(" select * from noit"); rs.next();但他们被宣布为null。第一个代码用于连接MySQL,第二个代码用于为按钮提供功能。
这是他的代码:
public class database {
public void con(){
Connection con = null;
Statement st = null;
ResultSet rs = null;
String nqkuvsi = null;
try{
con = (Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/userlogin","root","1234");
if(con!=null){
System.out.println("successfull");
}}catch(Exception e){
System.out.println("no connection");
e.printStackTrace();
}
}
}
第二段代码:
public void actionPerformed(ActionEvent a){
db.con();
ResultSet res = null;
int resul = 0;
Object s = a.getSource();
if(s==jbut){
Random rand = new Random();
Random rand1 = new Random();
int rnd = rand.nextInt(2)+1;
int rnd1 = rand1.nextInt(2) + 2;
String nqkuv = null;
String nqkuv1 = null,nqkuv2 = null,nqkuv3 = null;
if(rnd==1){
try{
st = con.createStatement();
rs = st.executeQuery("select * from noit");
rs.next();
nqkuv = rs.getString(rnd1);
}catch(SQLException e){
e.printStackTrace();
}
任何帮助表示赞赏!