package dblx;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Sjk1_5 {
private static String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String url="jdbc:sqlserver://neverAgain\\neverAgain:1433;DatabaseName=Myschool";
private static String user="sa";
private static String password="111111";
static JFrame frm=new JFrame("数据库查询");
static JTextField jtf1=new JTextField();
static JTextField jtf2=new JTextField();
static JButton bt=new JButton();
static JLabel jlab=new JLabel();
static JLabel jlab1=new JLabel();
public static void main(String[] args) {
jlab.setText("学号");
jlab1.setText("性别");
bt.setText("查询");
frm.add(jlab);
frm.add(jtf1);
frm.add(jlab1);
frm.add(jtf2);
frm.add(bt);
GridLayout grid=new GridLayout(3,2);
frm.setLayout(grid);
jtf1.setBackground(Color.RED);
frm.setSize(300,220);
bt.setBounds(20, 300, 50, 50);
bt.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String xh=null;
String jg=null;
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
String selectsql="select * from Student where Sno=?";
xh=jtf1.getText();
System.out.println(xh);
try{
Class.forName(driver);
conn=DriverManager.getConnection(url,user,password);
ps=conn.prepareStatement(selectsql);
ps.setString(1,xh);
rs=ps.executeQuery();
while(rs.next()){
jg=rs.getString("Sex");
}
jtf2.setText(jg);
System.out.println(jg);
}
catch(Exception e1){
e1.printStackTrace();
}
finally{
try{
if(rs!=null)rs.close();
if(ps!=null)ps.close();
if(conn!=null)conn.close();
}
catch(Exception e1)
{
e1.printStackTrace();
}
}
}
}
);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setVisible(true);
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70007877/viewspace-2795224/ ,如需转载,请注明出处,否则将追究法律责任。