百货商场信息管理系统
源代码在线查看: java-数据库连接方法.txt
//数据库连接方法
import java.sql.*;
Connection con;
Statement st;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:shopping";
String username="";
String password="";
con=DriverManager.getConnection(url,username,password);
}catch(SQLException e)
{
e.printStackTrace();
}
catch(ClassNotFoundException e1)
{
e1.printStackTrace();
}
//数据库查询方法
try{
String sql="select * from 个人信用卡库 ";
st=con.createStatement();
ResultSet result=st.executeQuery(sql);
while(result.next())
{
number=result.getInt("商品代号");
price=result.getInt("商品单价");
name=result.getString("商品名称");
}
st.close();
}catch(SQLException e1)
{
JOptionPane.showMessageDialog(null,"数据库连接失败");
}
//数据库更新方法
try {
st = con.createStatement();
sql = "update 个人信用卡库 set 卡号='" + cardNumber + "',密码='" + password +
"',姓名='" + name + "',金额=" + leftMoney + " where 卡号='" + cardNumber +
"' ";
st.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "更新成功!");
}
catch (SQLException e1) {
JOptionPane.showMessageDialog(null, "数据库连接失败");
}
//数据库删除方法
String sql="delete * from 单价库 where 商品代号="+number;
try {
st = con.createStatement();
st.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "删除成功!");
st.close();
}
catch (SQLException e1) {
JOptionPane.showMessageDialog(null, "数据库连接失败");
}
}
//数据库插入方法
try {
st = con.createStatement();
sql = "insert into 个人信用卡库 values('" + cardNumber + "','" + password +
"','" + name + "'," + Money + ")";
st.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "添加成功!");
}
catch (SQLException e1) {
JOptionPane.showMessageDialog(null, "数据库连接失败");
}
}