java和mysql进行连接

来源:互联网 发布:怎么用语法创建数据库 编辑:程序博客网 时间:2024/06/04 18:03
package com.lovo.test;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;


import javax.swing.JOptionPane;


import com.lovo.bean.UserInformation;


public class WorkTest {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1、获取驱动器类
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
try {
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/instant", "root", "");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 获取连接对象的信息
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 增加
// String name = JOptionPane.showInputDialog(null, "请输入您要添加的用户账号");
// String pwd = JOptionPane.showInputDialog(null, "请输入您要添加的用户密码");
// String account = JOptionPane.showInputDialog(null, "请输入您要添加的用户金额");
// String sql =
// "insert into  t_user(f_username,f_password,f_account) values(?,?,?)";
// try {
// PreparedStatement ps = con.prepareStatement(sql);
// ps.setString(1, name);
// ps.setString(2, pwd);
// ps.setString(3, account);
// ps.executeUpdate();
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// 删除
// String name = JOptionPane.showInputDialog(null, "请输入您要删除的用户账号");
// String sql = "delete from t_user where f_username=?";
// try {
// PreparedStatement ps = con.prepareStatement(sql);
// ps.setString(1, name);
// ps.executeUpdate();
// System.out.println(sql);
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// 修改
String name = JOptionPane.showInputDialog(null, "请输入您要修改的用户账号");
String pwd = JOptionPane.showInputDialog(null, "请输入您要修改的用户密码");
String sql = "update t_user set f_password=? where f_username=?";
try {
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, pwd);
ps.setString(2, name);
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 单用户查找
// String inputname = JOptionPane.showInputDialog(null, "请输入您的用户账号");
// String inputpwd = JOptionPane.showInputDialog(null, "请输入您的用户密码");
// String sql =
// "select * from t_user where f_username=? and f_password=?";
// UserInformation theuser = null;
// ArrayList<UserInformation> alluesrs = new
// ArrayList<UserInformation>();
// try {
// PreparedStatement ps = con.prepareStatement(sql);
// ps.setString(1, inputname);
// ps.setString(2, inputpwd);
// ResultSet rs = ps.executeQuery();
// while (rs.next()) {
// theuser = new UserInformation();
// theuser.setId(rs.getInt(1));
// theuser.setUsername(rs.getString(2));
// theuser.setPwd(rs.getString(3));
// theuser.setAccount(rs.getDouble(4));
// }
// System.out.println(theuser.getUsername());
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } finally {
// if (con != null) {
// try {
// con.close();
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// }
// 多用户
// String sql = "select * from t_user where f_account>12000";
// UserInformation theuser = null;
// ArrayList<UserInformation> alluesrs = new
// ArrayList<UserInformation>();
// try {
// Statement sta = con.createStatement();
// ResultSet rs = sta.executeQuery(sql);
// while (rs.next()) {
// theuser = new UserInformation();
// theuser.setId(rs.getInt(1));
// theuser.setUsername(rs.getString(2));
// theuser.setPwd(rs.getString(3));
// theuser.setAccount(rs.getDouble(4));
// alluesrs.add(theuser);
// }
// System.out.println(alluesrs.get(0).getUsername());
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } finally {
// if (con != null) {
// try {
// con.close();
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// }
}
}
0 0
原创粉丝点击