JAVA数据库连接问题
检查密码是否正确的Checkpasswd类package com.qdgxy.sql;
import java.sql.SQLException;
public class Checkpasswd extends SQL_connection{
private static int i =0;
//select count(*) from consumer where consumer_password='12345678' and exists(
//select * from consumer where consumer_no=00001 )
//final String querystatement="select "+s1+" from "+s2;
public static void query(String s1,String s2)
{
SQL_connection.query(s1,s2);
System.out.println("query");
/* final String querystatement="select "+s1+" from "+s2;
try {
rs=stmt.executeQuery(querystatement);
} catch (SQLException e) {
e.getMessage();
}*/
}
public static void exe()
{
System.out.println("exe");
try{
i = rs.getInt(1);
System.out.println("exe");
System.out.println(i);
}catch(SQLException e) {
e.getMessage();
}
}
public static int i易做图ist()
{
System.out.println("i易做图ist");
if(i !=0)
return 0;//存在此账户
else
return 1;//不存在此账户
}
}
连接数据库的父类SQLException
package com.qdgxy.sql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SQL_connection {
/**
* @param args
* @throws ClassNotFoundException
* @throws SQLException
*/
/**
* @param args
*/
// TODO Auto-generated method stub
final static String Drivername="com.microsoft.sqlserver.jdbc.SQLServerDriver";
final static String Connection="jdbc:sqlserver://localhost:1433;DatabaseName=LaboratoryDatabase";
final static String Username="sa";
final static String Passwd="123";
protected static Connection conn=null;
protected static Statement stmt=null;
protected static ResultSet rs=null;
public static void classforname()
{
try {
Class.forName(Drivername);
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.getMessage();
}
}
public static void connection()
{
try{
conn=DriverManager.getConnection(Connection,Username,Passwd);
System.out.println("conn");
System.out.println(conn);
}catch(SQLException e){
e.getMessage();
}
}
public static void statement()
{
try {
stmt=conn.createStatement();
System.out.println("stmt");
System.out.println(stmt);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.getMessage();
}
}
public static void query(String s1,String s2)
//s1代表列名,s2代表表名
{
final String querystatement="select "+s1+" from "+s2;
try {
rs=stmt.executeQuery(querystatement);
System.out.println("rs");
System.out.println(rs);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.getMessage();
}
}
public static void exe()
{
/* try{
while(rs.next())
{
String s1=rs.getString(1);
System.out.println(s1);
}
}catch(SQLException e) {
e.getMessage();
} */
}
public static void close()
{
System.out.println("close");
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
String name=text.getText();
String passwd=text_1.getText();
final String s1="count(*)";
final String s2="consumer where consumer_password='"+passwd+"' and exists(select * from consumer where consumer_no='"+name+"');";
Checkpasswd.classforname();
Checkpasswd.connection();
Checkpasswd.statement();
Checkpasswd.query(s1,s2);
Checkpasswd.exe();
Checkpasswd.close();
if(Checkpasswd.i易做图ist() ==0)
{
System.out.println("new mainmenu");
shell.dispose();
new Mainmenu();
}
else{
create_worrymessagebox();
}
}
});
数据库语言为
select count(*) from consumer where consumer_password='12345678' and exists(
select * from consumer where consumer_no=00001 )
查询结果为(列名)无列名:(值)1
问题是checkpasswd里的
try{
i = rs.getInt(1);
System.out.println("exe");
System.out.println(i);
}catch(SQLException e) {
e.getMessage();
}
不执行,达不到我验证密码要求,为什么这段代码程序不执行,很疑惑,下面那些close()却继续执行了 --------------------编程问答-------------------- 写的真乱 !!!
-----------------
1、代码段的运行结果是什么,请帖出来,再来分析你的问题?
2、说一个跟你程序无关的问题,你这个Checkpasswd一直都是static ,我只能说呵呵了 ...
从我个人的理解来看,如果不是为了获取些什么必要的“封装”信息 , 何必用static ?
3、先看(2、),然后要说,你这里很多函数其实就一两句话的事,整这么多你不嫌累我们看得都郁闷!
可读性不好,所以我说你这个程序"写的真乱!" --------------------编程问答-------------------- 你的这个函数public static void exe()里面都被注释掉了啊,当然不执行了 --------------------编程问答--------------------
但是checkpasswd重写了SQLconnectionde exe()方法了啊 --------------------编程问答--------------------
conn
com.microsoft.sqlserver.jdbc.SQLServerConnection@c75e4fc
stmt
com.microsoft.sqlserver.jdbc.SQLServerStatement@11bbf1ca
rs
com.microsoft.sqlserver.jdbc.SQLServerResultSet@6a8c436b
query
exe
close
i易做图ist
上面是执行过程
刚学完面向对象的JAVA,见谅 --------------------编程问答--------------------
conn
com.microsoft.sqlserver.jdbc.SQLServerConnection@c75e4fc
stmt
com.microsoft.sqlserver.jdbc.SQLServerStatement@11bbf1ca
rs
com.microsoft.sqlserver.jdbc.SQLServerResultSet@6a8c436b
query
exe
close
i易做图ist
上面是执行过程 --------------------编程问答-------------------- 出错误没。debug 调试看看。 --------------------编程问答--------------------
建议你调试,一步步走,看哪一步出问题了,注意控制台打印出的异常 --------------------编程问答-------------------- 没看到rs.next()方法,rs是指针,next方法后才是第一个值。 --------------------编程问答-------------------- 为什么这么乱...
public class UserDaoImpl implements UserDao {
@Override
public User findWithLoginnameAndPassword(String loginname, String password)
throws SQLException {
Connection con = null;
PreparedStatement pstm = null;
ResultSet rs = null;
try {
con = ConnectionFactory.getConnection();
StringBuffer sql = new StringBuffer();
sql.append(" SELECT * FROM tb_user ");
sql.append(" WHERE loginname = ? ");
sql.append(" AND password = ? ");
System.out.println(sql.toString());
pstm = con.prepareStatement(sql.toString());
pstm.setString(1, loginname);
pstm.setString(2, password);
rs = pstm.executeQuery();
if(rs.next()){
User user = new User();
user.setId(rs.getInt("id"));
user.setPassword(password);
user.setLoginname(loginname);
user.setUsername(rs.getString("username"));
user.setAddress(rs.getString("address"));
user.setPhone(rs.getString("phone"));
user.setRole(rs.getInt("role"));
return user;
}
return null;
} catch (Exception e) {
}finally{
ConnectionFactory.close(con, pstm, rs);
}
return null;
}
}
数据库连接工厂
public class ConnectionFactory {
private static DataSource ds;
static{
try {
Context context = new InitialContext();
ds = (DataSource) context.lookup("java:/comp/env/jdbc/bookDB");
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
try {
return ds.getConnection();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void close(Connection con,Statement stm,ResultSet rs){
try {
if (rs != null) rs.close();
if (stm != null) stm.close();
if (con != null) con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
tomcat的context配置:
<Resource name="jdbc/bookDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/buy"/>
补充:Java , Java SE