当前位置: 代码迷 >> Eclipse >> java 连接数据库,该怎么解决
  详细解决方案

java 连接数据库,该怎么解决

热度:6   发布时间:2016-04-23 14:08:11.0
java 连接数据库
package neuq.zpr;

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.sql.*;


public class MyFrame extends JFrame implements ActionListener {

JTextField textuser;
JTextField textpass;
JLabel labeluser;
JLabel labelpass;
JButton btnSubmit;
void init()
{
labeluser=new JLabel("用户");
labelpass=new JLabel("密码");
textuser=new JTextField("",20);
textpass=new JTextField("",20);
btnSubmit=new JButton("确定");
add(labeluser);
add(textuser);
add(labelpass);
add(textpass);
add(btnSubmit);
btnSubmit.addActionListener(this);
}
public MyFrame()
{
init();
this.setLayout(new FlowLayout());
this.setBounds(100,100,190,150);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}



@Override
public void actionPerformed(ActionEvent e) {
String s1=textuser.getText();
String s2=textpass.getText();
Connection con;
java.sql.Statement sql;
ResultSet rs;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException s){
System.out.print(s);
}
try{
con=DriverManager.getConnection("jdbc:odbc:aaa","","");
sql=con.createStatement();
rs=sql.executeQuery("SELECT * FROM ll WHERE username='"+s1+"' AND password='"+s2+"'");

if(rs.next())
{
JOptionPane.showMessageDialog(this,"欢迎","正确",JOptionPane.INFORMATION_MESSAGE);

}
else
{
JOptionPane.showMessageDialog(this,"对不起","警告对话框",JOptionPane.WARNING_MESSAGE);

}
con.close();

}
catch(SQLException e1)
{
System.out.println(e1);
}
// TODO Auto-generated method stub

}

}
创建主类运行后是一下提示求各位大牛帮助解决

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] 标准表达式中数据类型不匹配。

------解决方案--------------------
我这里已经调试可以跑成功了,我的数据库文件是E:/books.accdb你调整为自己的
还有运行这个可能需要装一个Access的OLE和JDBC驱动,我安装了AccessDatabaseEngine2010
如果运行不起来,可以加我QQ 519582737帮你解决
对应的数据表我建立的是users表,跟你原来的程序不同
Java code
import java.awt.event.*;import java.awt.*;import javax.swing.*;import java.sql.*;import java.util.Properties;public class MyFrame extends JFrame implements ActionListener {    JTextField textuser;    JTextField textpass;    JLabel labeluser;    JLabel labelpass;    JButton btnSubmit;    void init()    {        labeluser=new JLabel("用户");        labelpass=new JLabel("密码");        textuser=new JTextField("",20);        textpass=new JTextField("",20);        btnSubmit=new JButton("确定");        add(labeluser);        add(textuser);        add(labelpass);        add(textpass);        add(btnSubmit);        btnSubmit.addActionListener(this);    }    public MyFrame()    {        init();        this.setLayout(new FlowLayout());//        this.setBounds(100,100,190,150);        this.pack();        this.setVisible(true);        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }    @Override    public void actionPerformed(ActionEvent e) {        String s1=textuser.getText();        String s2=textpass.getText();        String url="jdbc:odbc:DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=E:/books.accdb;";        Connection con;        java.sql.Statement sql;        ResultSet rs=null;        try{            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");        }        catch(ClassNotFoundException ee){            ee.printStackTrace();        }        try{            Properties p=new Properties();            p.put("charSet", "GBK");            con=DriverManager.getConnection(url,p);            sql=con.createStatement();            String query="SELECT * FROM users WHERE username='"+s1+"' AND password='"+s2+"'";            System.out.println("query:"+query);            rs=sql.executeQuery(query);            int i=0;            while(rs.next()){               System.out.println(rs.getInt("id"));                i++;            }            if(i>=1){                JOptionPane.showMessageDialog(this,"欢迎","正确",JOptionPane.INFORMATION_MESSAGE);            }else{                JOptionPane.showMessageDialog(this,"对不起","警告对话框",JOptionPane.WARNING_MESSAGE);            }            con.close();        }        catch(SQLException e1){            e1.printStackTrace();        }// TODO Auto-generated method stub    }    public static void main(String[] args) {        new MyFrame();    }}
  相关解决方案