1.从学生表中查询所有信息,并在控制台显示。
import java.sql.*;
public class Query1 {
public static void main(String[] args) {
//指定驱动,得到数据库连接
try{
String DBURL = "jdbc:sqlserver://localhost:1433;DataBaseName=student";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(DBURL,"sa","sa");
//创建执行语句对象
Statement stmt=conn.createStatement();
//执行SQL语句,得到结果
String sql="select * from stuInfo";
ResultSet rs=stmt.executeQuery(sql);
//处理结果集}
while(rs.next()){
String stuNo=rs.getString("stuNo");
String stuName=rs.getString("stuName");
int stuAge=rs.getInt("stuAge");
String stuSex=rs.getString("stuSex");
String stuClass=rs.getString("stuClass");
System.out.println(stuNo+""+stuName+""+stuAge+""+stuSex+""+stuClass);
} //关闭连接
rs.close();
stmt.close();
conn.close();
}catch(Exception ex){
System.out.println(ex);
}
}
}
2.插入行:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Insert1 {
public static void main(String[] args) {
try{
String DBURL = "jdbc:sqlserver://localhost:1433;DataBaseName=student";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(DBURL,"sa","sa");
Statement stmt=conn.createStatement();
String sql="insert into stuInfo values('2009007','李四','18','男','09会计')";
int i=stmt.executeUpdate(sql);
if(i==1)System.out.println("插入成功");
stmt.close();
conn.close();
}catch(Exception ex){
System.out.println(ex);
}
}
}
3.修改:
package chap1;
import java.sql.*;
public class Update2 {
public static void main(String[] args) {
try{
String DBURL = "jdbc:sqlserver://localhost:1433;DataBaseName=student";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(DBURL,"sa","sa");
Statement stmt=conn.createStatement();
String sql="update stuInfo set stuSex='女' where stuName='李四'";
int i=stmt.executeUpdate(sql);
System.out.println("修改了"+i+"行");
stmt.close();
conn.close();
}catch(Exception ex){
System.out.println(ex);
}
}
}
4.删除行:
package chap1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class delete3 {
public static void main(String[] args) {
try{
String DBURL = "jdbc:sqlserver://localhost:1433;DataBaseName=student";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(DBURL,"sa","sa");
Statement stmt=conn.createStatement();
String sql="delete from stuInfo where stuName='李四'";
int i=stmt.executeUpdate(sql);
System.out.println("删除了"+i+"行");
stmt.close();
conn.close();
}catch(Exception ex){
System.out.println(ex);
}
}
}
5. java中的数据库:
package chap1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class insert2 {
public static void main(String[] args) {
try{
String DBURL = "jdbc:sqlserver://localhost:1433;DataBaseName=student";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(DBURL,"sa","sa");
Statement stmt=conn.createStatement();
String s1="00000002";
String sql="insert into stuInfo values('"+s1+"','李四','18','男','09会计')";
int i=stmt.executeUpdate(sql);
System.out.println("插入"+i+"行");
stmt.close();
conn.close();
}catch(Exception ex){
System.out.println(ex);
}
}
}
二、实现学生注册界面的“注册”按钮的事件处理,添加学生信息到数据库中。
1.注册:
package yin;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
public class Register{
JFrame f;
Container con;
JPanel p1;
JLabel lab1,lab2,lab3,lab4,lab5;
JTextField t1,t2,t3;
JRadioButton rb1,rb2;
JComboBox cbo;
JButton b1,b2;
public Register(){
f=new JFrame("学生注册窗口");
con=f.getContentPane();
p1=new JPanel();
p1.setLayout(new FlowLayout());
lab1=new JLabel("学号");
lab2=new JLabel("姓名");
lab3=new JLabel("年龄");
lab4=new JLabel("性别 ");
lab5=new JLabel("班级 ");
t1=new JTextField("",12);
t2=new JTextField("",12);
t3=new JTextField("",12);
rb1=new JRadioButton("男 ",true);
rb2=new JRadioButton("女 ");
ButtonGroup bg=new ButtonGroup( );
bg.add(rb1);
bg.add(rb2);
String[] banji={"08电商一班","08电商二班","08计算机一班","08计算机二班"};
cbo=new JComboBox(banji);
b1=new JButton("注册");
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String sno=t1.getText();
String sname=t2.getText();
int age=Integer.parseInt(t3.getText());
String sex;
if(rb1.isSelected()){
sex="男";
}else{
sex="女";
}
String edu=""+cbo.getSelectedItem();
try{
String DBURL = "jdbc:sqlserver://localhost:1433;DataBaseName=student";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(DBURL,"sa","sa");
Statement stmt=conn.createStatement();
String sql="insert into stuInfo values('"+sno+"','"+sname+"','"+age+"','"+sex+"','"+edu+"')";
int i=stmt.executeUpdate(sql);
if(i==1)
JOptionPane.showMessageDialog(null, "注册成功");
stmt.close();
conn.close();
}catch(Exception ex){
System.out.println(ex);
}
}
});
b2=new JButton("取消");
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
p1.add(lab1);
p1.add(t1);
p1.add(lab2);
p1.add(t2);
p1.add(lab3);
p1.add(t3);
p1.add(lab4);
p1.add(rb1);
p1.add(rb2);
p1.add(lab5);
p1.add(cbo);
p1.add(b1);
p1.add(b2);
con.add(p1);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(200,250);
f.setResizable(false);
f.setVisible(true);
}
public static void main(String[] args){
new Register();
}
}
2.查询:
package yin;
import java.sql.*;
public class Query {
public static void main(String[] args) {
try{
String DBURL = "jdbc:sqlserver://localhost:1433;DataBaseName=student";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(DBURL,"sa","sa");
Statement stmt=conn.createStatement();
String sql="select * from stuInfo";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
String stuNo=rs.getString("stuNo");
String stuName=rs.getString("stuName");
String stuSex=rs.getString("stuSex");
int stuAge=rs.getInt("stuAge");
String stuClass=rs.getString("stuClass");
System.out.println(stuNo+" "+stuName+" "+stuAge+" "+stuSex+" "+stuClass);
} rs.close();
stmt.close();
conn.close();
}catch(Exception ex){
System.out.println(ex);
}
}
}
3.清空:
package yin;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
public class Register{
JFrame f;
Container con;
JPanel p1;
JLabel lab1,lab2,lab3,lab4,lab5;
JTextField t1,t2,t3;
JRadioButton rb1,rb2;
JComboBox cbo;
JButton b1,b2;
public Register(){
f=new JFrame("学生注册窗口");
con=f.getContentPane();
p1=new JPanel();
p1.setLayout(new FlowLayout());
lab1=new JLabel("学号");
lab2=new JLabel("姓名");
lab3=new JLabel("年龄");
lab4=new JLabel("性别 ");
lab5=new JLabel("班级 ");
t1=new JTextField("",12);
t2=new JTextField("",12);
t3=new JTextField("",12);
rb1=new JRadioButton("男 ",true);
rb2=new JRadioButton("女 ");
ButtonGroup bg=new ButtonGroup( );
bg.add(rb1);
bg.add(rb2);
String[] banji={"08电商一班","08电商二班","08计算机一班","08计算机二班"};
cbo=new JComboBox(banji);
b1=new JButton("注册");
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String sno=t1.getText();
String sname=t2.getText();
int age=Integer.parseInt(t3.getText());
String sex;
if(rb1.isSelected()){
sex="男";
}else{
sex="女";
}
String edu=""+cbo.getSelectedItem();
try{
String DBURL = "jdbc:sqlserver://localhost:1433;DataBaseName=student";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(DBURL,"sa","sa");
Statement stmt=conn.createStatement();
String sql="insert into stuInfo values('"+sno+"','"+sname+"','"+age+"','"+sex+"','"+edu+"')";
int i=stmt.executeUpdate(sql);
if(i==1){
JOptionPane.showMessageDialog(null, "注册成功");
}
conn.close();
t1.setText("");
t2.setText("");
t3.setText("");
rb1.setSelected(true);
cbo.setSelectedIndex(0);
}catch(Exception ex){
ex.printStackTrace();
}
}
});
b2=new JButton("取消");
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
p1.add(lab1);
p1.add(t1);
p1.add(lab2);
p1.add(t2);
p1.add(lab3);
p1.add(t3);
p1.add(lab4);
p1.add(rb1);
p1.add(rb2);
p1.add(lab5);
p1.add(cbo);
p1.add(b1);
p1.add(b2);
con.add(p1);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(200,250);
f.setResizable(false);
f.setVisible(true);
}
public static void main(String[] args){
new Register();
}
}
通讯录