日期:2014-05-20  浏览次数:20737 次

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  isexist()
{
System.out.println("isexist");
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.isexist() ==0)
{
System.out.println("new mainmenu");
shell.dispose();
new Mainmenu();


  else{
  create_worrymessagebox();