📄 yuangong.java
字号:
package yuan.xiaoshou;
import xiao.xiaoshouxiaoshou.*;
import sun.BuMenXiaoShou.*;
import sn.ChangPingXiaoShou.*;
import deng.shaoxiao.*;
import com.yonghuxiaoshou.*;
import cn.KeKuXiaoShao.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//员工管理数据库
import java.sql.*;
import java.util.ArrayList;
import java.util.*;
public class YuanGong {
//定义一个类对其进行连接数据库
//驱动类
private static final String sDBDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//连接字符串
private static final String url = "jdbc:sqlserver://localhost:1433;databasename = XiaoShou";
//连接姓名
private static final String user = "zhou";
//连接密码
private static final String pwd = "111111";
//连接发送集
private static PreparedStatement ps = null;
//连接结果集
private static ResultSet rs = null;
//定义一个连接的方法
public static Connection getconn(){
Connection conn = null;
try{
Class.forName(sDBDriver);
conn = DriverManager.getConnection(url,user,pwd);
}
catch(java.lang.ClassNotFoundException e){
System.out.println ("找不到文件");
}
catch(SQLException e){
System.out.println ("连接失败");
}
return conn;
}
//1定义一个查询的方法(不带参数)
public static ResultSet select(){
Connection conn = getconn();
try{
ps = conn.prepareStatement("select * from t_emp");
rs = ps.executeQuery();
}
catch(SQLException e){
System.out.println ("查询失败");
}
return rs;
}
//2定义一个添加的方法
public static int insert(String T_realname,int T_pid,int T_sex,String T_address,int T_age,String T_tel){
Connection conn = getconn();
int num = 0;
try{
ps = conn.prepareStatement("insert into t_emp values(?,?,?,?,?,?)");
ps.setString(1,T_realname);
ps.setInt(2,T_pid);
ps.setInt(3,T_sex);
ps.setString(4,T_address);
ps.setInt(5,T_age);
ps.setString(6,T_tel);
num = ps.executeUpdate();
}
catch(SQLException e){
System.out.println ("添加失败");
}
finally{
try{
ps.close();
conn.close();
}
catch(SQLException e){
System.out.println ("sql添加失败");
}
}
return num;
}
//3定义一个修改的方法
public static int update(int ID,String T_realname,int T_pid,int T_sex,String T_address,int T_age,String T_tel,int ID1){
Connection conn = getconn();
int num = 0;
try{
ps = conn.prepareStatement("update t_emp set T_realname = ?,T_pid = ?,T_sex = ?,T_address = ?,T_age = ?,T_tel = ? where ID = ?");
// ps.setInt(1,ID);
ps.setString(1,T_realname);
ps.setInt(2,T_pid);
ps.setInt(3,T_sex);
ps.setString(4,T_address);
ps.setInt(5,T_age);
ps.setString(6,T_tel);
ps.setInt(7,ID1);
num =ps.executeUpdate();
}
catch(SQLException e){
System.out.println ("修改失败");
}
finally{
try{
ps.close();
conn.close();
}
catch(SQLException e){
System.out.println ("sql修改失败");
}
}
return num;
}
//4定义一个删除的方法不带参数的删除方法
public static int delete(){
Connection conn = getconn();
int num = 0;
try{
ps = conn.prepareStatement("delete from t_emp");
num = ps.executeUpdate();
}
catch(SQLException e){
System.out.println ("删除失败");
}
finally{
try{
ps.close();
conn.close();
}
catch(SQLException e){
System.out.println ("sql删除失败");
}
}
return num;
}
//4定义一个代参数的删除方法
public static int delete(int ID){
Connection conn = getconn();
int num = 0;
try{
ps = conn.prepareStatement("delete from t_emp where ID = ?");
ps.setInt(1,ID);
num = ps.executeUpdate();
}
catch(SQLException e){
System.out.println ("带参数删除失败");
}
finally{
try{
ps.close();
conn.close();
}
catch(SQLException e){
System.out.println ("sql带参数删除失败");
}
}
return num;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -