📄 chanping.java
字号:
package sn.ChangPingXiaoShou;
import cn.KeKuXiaoShao.*;
import com.yonghuxiaoshou.*;
import deng.shaoxiao.*;
import sun.BuMenXiaoShou.*;
import xiao.xiaoshouxiaoshou.*;
import yuan.xiaoshou.*;
//定义一个产品信息表连接库
import java.sql.*;
import java.util.*;
public class ChanPing {
//驱动类
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();
ArrayList arr = new ArrayList();
try{
ps = conn.prepareStatement("select * from t_products");
rs = ps.executeQuery();
}
catch(SQLException e){
System.out.println ("查询失败");
}
return rs;
}
//1定义一个带参数的查询方法
public static ArrayList select(int D_id){
Connection conn = getconn();
ArrayList arr = new ArrayList();
try{
ps = conn.prepareStatement("select * from t_products where D_id = ?");
ps.setInt(1,D_id);
rs = ps.executeQuery();
if(rs != null){
while(rs.next()){
arr.add(rs.getInt(1));
arr.add(rs.getString(2));
arr.add(rs.getString(3));
arr.add(rs.getString(4));
}
}
}
catch(SQLException e){
System.out.println ("带参数查询失败");
}
finally{
try{
ps.close();
conn.close();
}
catch(SQLException e){
System.out.println ("sql带参数查询失败");
}
}
return arr;
}
//2定义一个添加的方法
public static int insert(int D_id,String D_name,String D_price,String D_remark){
Connection conn = getconn();
int num = 0;
try{
ps = conn.prepareStatement("insert into t_products values(?,?,?,?)");
ps.setInt(1,D_id);
ps.setString(2,D_name);
ps.setString(3,D_price);
ps.setString(4,D_remark);
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 D_id,String D_name,String D_price,String D_remark,int D_id1){
Connection conn = getconn();
int num = 0;
try{
ps = conn.prepareStatement("update t_products set D_name = ?,D_price = ?,D_remark = ? where D_id = ?");
ps.setString(1,D_name);
ps.setString(2,D_price);
ps.setString(3,D_remark);
ps.setInt(4,D_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定义一个删除的方法不带参数的删除方法
// private int delete(){
// Connection conn = getconn();
// int num = 0;
// try{
// ps = conn.prepareStatement("delete from t_products");
// 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 D_id){
Connection conn = getconn();
int num = 0;
try{
ps = conn.prepareStatement("delete from t_products where D_id = ?");
ps.setInt(1,D_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 + -