package drivers;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Main implements ActionListener
{
private JFrame f;
private JLabel lb_name,lb_email,lb_address,lb_hourlyrate,lb_startdate,lb_enddate,lb_reason,lb_homeph,lb_cellph,lb_image;
private JTextField tx_name,tx_email,tx_address,tx_hourlyrate,tx_startdate,tx_enddate,tx_homeph,tx_cellph;
private JButton bt_go,bt_next,bt_image;
private JTextArea txf_reason;
private ImageIcon ic;
private JPanel p;
public String s="";
public Main()
{
f=new JFrame();
lb_name=new JLabel("Name");
lb_email=new JLabel("E-mail");
lb_address=new JLabel("Address");
lb_hourlyrate=new JLabel("Hourly Rate");
lb_startdate=new JLabel("Starting Date");
lb_enddate=new JLabel("Enddate");
lb_reason=new JLabel("Reason of Quiting");
lb_homeph=new JLabel("Home Phone");
lb_cellph=new JLabel("Cell Phone");
tx_name=new JTextField();
tx_email=new JTextField();
tx_address=new JTextField();
tx_hourlyrate=new JTextField();
tx_startdate=new JTextField();
tx_enddate=new JTextField();
txf_reason=new JTextArea();
tx_homeph=new JTextField();
tx_cellph=new JTextField();
bt_image=new JButton("Browse");
bt_image.addActionListener(this);
bt_go=new JButton("Go");
bt_go.addActionListener(this);
bt_next=new JButton("Next");
bt_next.addActionListener(this);
ic =new ImageIcon(s);
lb_image=new JLabel();
lb_image.setIcon(ic);
p=new JPanel();
p.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255,255,255), 1, true));
}
public void launch()
{
f.setLayout(null);
f.setSize(630,470);
f.add(lb_name);
f.add(lb_email);
f.add(lb_address);
f.add(lb_hourlyrate);
f.add(lb_startdate);
f.add(lb_enddate);
f.add(lb_reason);
f.add(lb_homeph);
f.add(lb_cellph);
f.add(tx_name);
f.add(tx_email);
f.add(tx_address);
f.add(tx_hourlyrate);
f.add(tx_startdate);
f.add(tx_enddate);
f.add(txf_reason);
f.add(tx_homeph);
f.add(tx_cellph);
f.add(bt_go);
f.add(bt_next);
f.add(p);
p.add(lb_image);
p.setBackground(Color.BLACK);
lb_name.setBounds(20,30,50,30);
tx_name.setBounds(60,30,150,30);
bt_go.setBounds(270,30,60,30);
lb_email.setBounds(20,110,40,30);
tx_email.setBounds(130,113,200,25);
lb_address.setBounds(20,140,50,30);
tx_address.setBounds(130,143,200,25);
lb_hourlyrate.setBounds(20,170,70,30);
tx_hourlyrate.setBounds(130,173,60,25);
lb_startdate.setBounds(20,200,80,30);
tx_startdate.setBounds(130,203,150,25);
lb_enddate.setBounds(20,230,80,30);
tx_enddate.setBounds(130,233,150,25);
lb_homeph.setBounds(20,260,80,30);
tx_homeph.setBounds(130,263,100,25);
lb_cellph.setBounds(20,290,80,30);
tx_cellph.setBounds(130,293,100,25);
lb_reason.setBounds(20,320,100,30);
txf_reason.setBounds(130,323,200,70);
bt_next.setBounds(400,380,60,40);
p.setBounds(350,100,220,240);
tx_email.setEditable(false);
tx_address.setEditable(false);
tx_hourlyrate.setEditable(false);
tx_startdate.setEditable(false);
tx_enddate.setEditable(false);
tx_homeph.setEditable(false);
tx_cellph.setEditable(false);
txf_reason.setEditable(false);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
Main m=new Main();
m.launch();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(bt_go))
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =DriverManager.getConnection("jdbc:odbc:project","sa","123456");
String s=tx_name.getText();
Statement stmt= con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT name,address,email,startdate,enddate,reason,homeph,cellph,hourlyrate,image FROM driver");
while(rs.next())
{
if(s.equals(rs.getString(1)))
{
tx_address.setText(rs.getString(2));
tx_email.setText(rs.getString(3));
tx_startdate.setText(rs.getString(4));
tx_enddate.setText(rs.getString(5));
txf_reason.setText(rs.getString(6));
tx_homeph.setText(Integer.toString(rs.getInt(7)));
tx_cellph.setText(Integer.toString(rs.getInt(8)));
tx_hourlyrate.setText(Integer.toString(rs.getInt(9)));
s=rs.getString(10);
ic=new ImageIcon(s);
lb_image.setIcon(ic);
lb_image.setBounds(350,100,200,200);
break;
}
}
}
catch(Exception ex)
{
JOptionPane.showConfirmDialog(f,ex);
}
}
else if(e.getSource().equals(bt_next))
{
enter en=new enter();
en.launch();
f.hide();
}
else if(e.getSource().equals(bt_image))
{
String s;
JFileChooser jfc = new JFileChooser();
jfc.showDialog(jfc, "Choose an image file");
s = jfc.getSelectedFile().getAbsolutePath();
imagePath.setText(s);
}
}
}
我正在从数据库中搜索一个名称,并根据输入的名称在框架上显示它的信息,并且还有一个框架将值输入数据库。我把varhcar(10)作为名字的数据类型。例如如果我以名义输入rahul然后它被存储为rahul,后面有5个空格('rahul')。所以当我通过rahul名称搜索数据库时,它将不会显示数据,直到我输入5个空格后。我应该使用哪种数据类型,以使其长度与名称长度的长度相同???
这是我的SQL插入代码
INSERT INTO [project].[dbo].[driver]
([Name]
,[Address]
,[Email]
,[Hourlyrate]
,[Homeph]
,[Cellph]
,[Startdate]
,[Enddate]
,[Reason]
,[Image])
VALUES
(<Name, varchar(20),>
,<Address, varchar(50),>
,<Email, varchar(25),>
,<Hourlyrate, int,>
,<Homeph, bigint,>
,<Cellph, bigint,>
,<Startdate, varchar(30),>
,<Enddate, varchar(30),>
,<Reason, varchar(50),>
,<Image, varchar(200),>)