数据库验证
package mybean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Handle {
public boolean check(String u,String p) throws SQLException{
Connection conn=null;
Statement st=null;
ResultSet rs=null;
Boolean b=false;
try {
conn=new Connection().getConn();//建立连接
String sql="select * from table1 where username='"+u+"' and password="+p+"'";
System.out.print(sql+"111111111111");
st=conn.createStatement();
rs=st.executeQuery(sql);
if(rs.next()){
if(rs.getString(1).equals(p))
{
if(rs.getString(2).equals("1")){
b=true;
}
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
rs.close();
st.close();
}
return b;
}
}
一直报错”The method createStatement() is undefined for the type Connection“哪位大侠帮小弟看看。 createStatement对象
[解决办法]
你都没有import进来
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
[解决办法]
createStatement 没有此方法,是不是你的jar 错了。conn=new Connection().getConn();
不知道你封装的方法里怎么写的,现在是无法创建Statement
[解决办法]
/*
* u
*/
package wrapper;
import com.mysql.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author Administrator
*/
public abstract class Database {
/**
* 连接数据库
*/
public static Connection connect() throws SQLException, Exception{
// URL指向要访问的数据库名jdbc_test
String url = Config.getConfig("database.url");
// MySQL配置时的用户名
String user = Config.getConfig("database.user");
// Java连接MySQL配置时的密码
String password = Config.getConfig("database.password");
//这句可以没有
DriverManager.registerDriver(new Driver());
// 连接数据库
Connection connection = DriverManager.getConnection(url, user, password);
if (connection.isClosed()) {
throw new Exception("connect to database failed.");
}
//设置数据库的字符集
Statement statement = connection.createStatement();
statement.execute("SET character_set_client=`" + Config.getConfig("database.character_set_client") + "`");
statement.execute("SET character_set_connection=`" + Config.getConfig("database.character_set_connection") + "`");
statement.execute("SET character_set_results=`" + Config.getConfig("database.character_set_results") + "`");
statement.close();
//
return connection;
}
}