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();
// }
// }
// }
}
}
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
- java和mysql进行连接
- Java对MySQL数据库进行连接、查询和修改
- Java对MySQL数据库进行连接、查询和修改
- Java对MySQL数据库进行连接、查询和修改
- Java对MySQL数据库进行连接、查询和修改
- Java对MySQL数据库进行连接、查询和修改
- Java对MySQL数据库进行连接、查询和修改
- Java对MySQL数据库进行连接、查询和修改
- Java对MySQL数据库进行连接、查询和修改
- Java对MySQL数据库进行连接、查询和修改
- JAVA对Mysql进行连接、插入…
- java 连接mysql数据库 并进行操作
- java jdbc连接数据库(使用mysql进行连接)
- eclipse(Java)对MySQL数据库进行连接、查询和修改
- Java 连接MySQL数据库 和连接 SQLServer2005
- 基础--JAVA对Mysql进行连接、插入、修改、删除操作
- JAVA连接到MySql,并进行增删改查操作
- JAVA对MYSQL进行连接、插入、修改、删除操作
- Can't find keyplane that supports type 4 for keyboard iPhone-PortraitChoco-NumberPad; using 27057872
- UNIX环境高级编程之第5章:标准I/O库
- 关于对象深拷贝的一个方法(非Cloneable方式)
- ant安装配置,一个ant的简单实例
- netty学习(二)--传统的bio编程
- java和mysql进行连接
- 多态
- Foundation框架—结构体
- 水仙花数
- Centos 安装后无法使用ipv4地址
- RT-Thread finsh源码分析: finsh_node.h
- 集合的知识点
- bootstrap的多选下拉框的使用
- YYYY-mm-dd HH:MM:SS大小写解释