这是我用main函数运行的,可以输出ok
import java.util.*;
import java.net.*;
import java.sql.*;
public class dbtest {
public dbtest() {
}
public Connection getCon() throws ClassNotFoundException, SQLException {
Connection con=null;
try {
Class.forName("com.mysql.jdbc.Driver");
String URL="jdbc:mysql://localhost:3306/dormitory?user=root&password=goodgoodstudy";
con=DriverManager.getConnection(URL);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public ResultSet DBQuery(String sqlText) {
Connection connl=null;
ResultSet rs=null;
try {
connl=getCon();
Statement stmt=connl.createStatement();
rs=stmt.executeQuery(sqlText);
} catch (Exception x) {
x.printStackTrace();
}
return rs;
}
public int DBUpdate(String sqlText) {
Connection connl=null;
int rowcount=0;
try {
connl=getCon();
Statement stmt=connl.createStatement();
rowcount=stmt.executeUpdate(sqlText);
} catch (Exception x) {
x.printStackTrace();
}
return rowcount;
}
public static void main(String[] args) {
dbtest myconnect = new dbtest();
String name="zhouqi";
String password="hello";
if (name!=null && password!=null) {
ResultSet rs;
String sqlText="select * from user where name= '"+name+"'and password='"+password+"';";
rs=myconnect.DBQuery(sqlText);
try {
if (rs!=null && rs.next()==true) {
System.out.println("ok");
}
else {
System.out.println("用户"+name+"验证失败!");
}
} catch (Exception x) {
x.printStackTrace();
}
}
}
}
import java.util.*;
import java.net.*;
import java.sql.*;
public class dbtest {
public dbtest() {
}
public Connection getCon() throws ClassNotFoundException, SQLException {
Connection con=null;
try {
Class.forName("com.mysql.jdbc.Driver");
String URL="jdbc:mysql://localhost:3306/dormitory?user=root&password=goodgoodstudy";
con=DriverManager.getConnection(URL);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public ResultSet DBQuery(String sqlText) {
Connection connl=null;
ResultSet rs=null;
try {
connl=getCon();
Statement stmt=connl.createStatement();
rs=stmt.executeQuery(sqlText);
} catch (Exception x) {
x.printStackTrace();
}
return rs;
}
public int DBUpdate(String sqlText) {
Connection connl=null;
int rowcount=0;
try {
connl=getCon();
Statement stmt=connl.createStatement();
rowcount=stmt.executeUpdate(sqlText);
} catch (Exception x) {
x.printStackTrace();
}
return rowcount;
}
public static void main(String[] args) {
dbtest myconnect = new dbtest();
String name="zhouqi";
String password="hello";
if (name!=null && password!=null) {
ResultSet rs;
String sqlText="select * from user where name= '"+name+"'and password='"+password+"';";
rs=myconnect.DBQuery(sqlText);
try {
if (rs!=null && rs.next()==true) {
System.out.println("ok");
}
else {
System.out.println("用户"+name+"验证失败!");
}
} catch (Exception x) {
x.printStackTrace();
}
}
}
}









