客户信息可以存在数据库里面,增加的时候,通过代码连接上mysql数据库,然后拼接sql语句,查询或者增加修改数据库的字段和数据。连接数据库示例:
// 驱动程序名String driver = "com.mysql.jdbc.Driver"
// URL指向要访问的数据库名scutcs
String url = "jdbc:mysql://127.0.0.1:3306/scutcs"
// MySQL配置时的用户名
String user = "root"
// MySQL配置时的密码
String password = "root"
try {
// 加载驱动程序
Class.forName(driver)
// 连续数据库
Connection conn = DriverManager.getConnection(url, user, password)
if(!conn.isClosed())
System.out.println("Succeeded connecting to the Database!")
// statement用来执行SQL语句
Statement statement = conn.createStatement()
// 要执行的SQL语句
String sql = "select * from student"
// 结果集
ResultSet rs = statement.executeQuery(sql)
System.out.println("-----------------")
System.out.println("执行结果如下所示:")
System.out.println("-----------------")
System.out.println(" 学号" + "\t" + " 姓名")
System.out.println("-----------------")
//这个是我写的,里面有连接数据库的部分。你可以拿去参考一下
import java.awt.*
import javax.swing.*
import java.awt.event.*
import java.sql.*
class LoginFrm extends JFrame implements ActionListener// throws Exception
{
JLabel lbl1 = new JLabel("用户名:")
JLabel lbl2 = new JLabel("密码:")
JTextField txt = new JTextField(5)
JPasswordField pf = new JPasswordField()
JButton btn1 = new JButton("确定")
JButton btn2 = new JButton("取消")
public LoginFrm() {
this.setTitle("登陆")
JPanel jp = (JPanel) this.getContentPane()
jp.setLayout(new GridLayout(3, 2, 5, 5))
jp.add(lbl1)
jp.add(txt)
jp.add(lbl2)
jp.add(pf)
jp.add(btn1)
jp.add(btn2)
btn1.addActionListener(this)
btn2.addActionListener(this)
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btn1) {
try {
Class.forName("com.mysql.jdbc.Driver")// mysql数据库
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost/Car_zl", "root", "1")// 数据库名为Car_zl,密码为1
System.out.println("com : "+ con)
Statement cmd = con.createStatement()
String sql = "select * from user where User_ID='"
+ txt.getText() + "' and User_ps='"
+ pf.getText() + "'"
ResultSet rs = cmd
.executeQuery(sql)// 表名为user,user_ID和User_ps是存放用户名和密码的字段名
if (rs.next()) {
JOptionPane.showMessageDialog(null, "登陆成功!")
} else
JOptionPane.showMessageDialog(null, "用户名或密码错误!")
} catch (Exception ex) {
}
if (ae.getSource() == btn2) {
System.out.println("1111111111111")
//txt.setText("")
//pf.setText("")
System.exit(0)
}
}
}
public static void main(String arg[]) {
JFrame.setDefaultLookAndFeelDecorated(true)
LoginFrm frm = new LoginFrm()
frm.setSize(400, 200)
frm.setVisible(true)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)