我必须将ms访问数据库中的数据显示到标签。表包含这些列名称
ID(Long Integer),
Name(Text),)
Gender(Text),
Address(Text),
City (Text).
用户选择组合框值,这些值是来自表的ID,并且相应地显示相应的数据。错误是:
sucess sssssucess
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.
package studentdetails;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.sql.*;
import java.awt.event.KeyAdapter.*;
import java.awt.event.ActionListener.*;
import javax.swing.AbstractButton.*;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
class StudentDetails extends JFrame implements ActionListener,KeyListener
{
JLabel l1,l2,l3,l4,l5,l6,l7,l8,l9;
JLabel ll1,ll2,ll3,ll4,ll5,ll6,ll7;
JButton b1,b2,b3;
JComboBox c1;
Connection conn;
public StudentDetails()
{
super("Institute Management System");
l1=new JLabel("ID::");
l2=new JLabel("STUDENT NAME::");
l3=new JLabel("GENDER::");
l4=new JLabel("ADDRESS::");
l5=new JLabel("CITY::");
l6=new JLabel("CONTACT NO.::");
//l7=new JLabel("DATE OF JOINING::");
l8=new JLabel("STUDENT DETAILS::");
l9=new JLabel("SELECT THE STUDENT ID:");
ll1 = new JLabel();
ll2 = new JLabel();
ll3 = new JLabel();
ll4 = new JLabel();
ll5 = new JLabel();
ll6 = new JLabel();
//ll7 = new JLabel();
b1=new JButton("VIEW");
b2=new JButton("BACK");
b3=new JButton("CLEAR");
c1=new JComboBox();
add(l8);
l8.setBounds(210,30,150,80);
add(l9);
l9.setBounds(55,75,150,80);
add(c1);
c1.setBounds(250,100,150,30);
add(l1);
l1.setBounds(120,150,150,30);
add(ll1);
ll1.setBounds(250,150,150,30);
add(l2);
l2.setBounds(120,200,150,30);
add(ll2);
ll2.setBounds(250,200,150,30);
add(l3);
l3.setBounds(120,250,150,30);
add(ll3);
ll3.setBounds(250,250,150,30);
add(l4);
l4.setBounds(120,300,150,30);
add(ll4);
ll4.setBounds(250,300,150,30);
add(l5);
l5.setBounds(120,350,150,30);
add(ll5);
ll5.setBounds(250,350,150,30);
/*add(l6);
l6.setBounds(120,400,150,30);
add(ll6);
ll6.setBounds(250,400,150,30);
add(l7);
l7.setBounds(120,450,150,30);
add(ll7);
ll7.setBounds(250,450,150,30);
*/
add(b1);
b1.setBounds(500,100,80,30);
add(b2);
b2.setBounds(200,520,80,30);
add(b3);
b3.setBounds(450,520,80,30);
setSize(700,700);
setBackground(Color.BLUE);
setLayout(null);
setVisible(true);
setExtendedState(MAXIMIZED_BOTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:LANGUAGEDSN");
System.out.println("sucess");
Statement stmt=conn.createStatement();
String str = "Select ID from STUDENTDETAILS";
ResultSet rs = stmt.executeQuery(str);
while(rs.next())
{
c1.addItem(rs.getString(1));
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public void actionPerformed(ActionEvent ae)
{
Object ob=ae.getSource();
String i = (String)c1.getSelectedItem();
//SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm", Locale.getDefault());
//String sdt_ = sdf.format(dt_);
if(ob==b1)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:LANGUAGEDSN");
Statement stmt=conn.createStatement();
String st = "Select * from STUDENTDETAILS where ID ='"+i+"'";
System.out.println("sssssucess");
ResultSet rs = stmt.executeQuery(st);
while(rs.next())
{
l1.setText(rs.getString(1));
ll2.setText(rs.getString(2));
ll3.setText(rs.getString(3));
ll4.setText(rs.getString(4));
ll5.setText(rs.getString(5));
}
}
catch(Exception e)
{
System.out.println(e);
}
}
else if(ob==b2)
{
ll1.setText("");
ll2.setText("");
ll3.setText("");
ll4.setText("");
ll5.setText("");
//ll6.setText("");
//ll7.setText("");
}
}
public static void main(String args[])
{
new StudentDetails();
}
@Override
public void keyTyped(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void keyPressed(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void keyReleased(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
答案 0 :(得分:1)
首先,使用PreparedStatement
s
第二个...在引号中包含您的id值,'+i+'
将使Access(以及几乎所有数据库)将值视为String
,我怀疑它是什么。尝试删除引号
String st = "Select * from STUDENTDETAILS where ID =" + i;
第三......像素完美布局是现代UI开发中的一种幻觉。您无法控制不同平台的渲染管道。这将使你的像素完美,并变成乱码。
Swing旨在与布局管理器配合使用,这些功能使您能够专注于工作流程,而不是处理每种可能的DPI,字体大小和其他渲染问题的组合。
花点时间学习和使用合适的布局管理器,并查看Laying Out Components Within a Container了解更多详情