JDBC 连接数据库实例(MySQL为例)
2015-07-31 14:36
597 查看
比较常规的利用 JDBC 进行数据库操作的几个例子,写下来以防需要。方法肯定不是最好的,此处只是给出几个基本的代码,具体可以根据需要进行优化或者整合。
并且 jdbc 绝对不止这么一点简单的东西。如果熟悉其中的类和方法,可以达到更好的操作体验。
1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
2.建立连接
String url = "jdbc:mysql://localhost:3306/java?user = root&password = root";
Connection conn = DriverManager.getConnection(url, "root", "root");
3.增删改查
1)查:
public void select() throws SQLException {
try {
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM new_table";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.print(rs.getInt(1)+" ");
System.out.print(rs.getString(2)+" ");
System.out.println(rs.getInt(3));
}
} catch (SQLException e) {
System.out.println("__Select Error__");
e.printStackTrace();
}finally {
rs.close();
stmt.close();
}
}
2)删
public void delete(int id) throws SQLException {
try{
Statement stmt = conn.createStatement();
String sql = "DELETE FROM new_table WHERE id =?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1,id);
ps.executeUpdate();
} catch (SQLException e) {
System.out.println("__Delete Error__");
e.printStackTrace();
}finally {
ps.close();
stmt.close();
}
}
3)增
public void insert(User user) throws SQLException {
try{
int i = 0;
Statement stmt = conn.createStatement();
sql = "INSERT INTO new_table(id, name, age) value(?,?,?)";
ps = (PreparedStatement)conn.prepareStatement(sql);
ps.setInt(1, user.getId());
ps.setString(2,user.getName());
ps.setInt(3, user.getAge());
i = ps.executeUpdate();
} catch (SQLException e) {
System.out.println("__Insert Error__");
e.printStackTrace();
}finally {
ps.close();
stmt.close();
}
}
4)改
public void update(User user) throws SQLException{
try{
int i= 0;
Statement stmt = conn.createStatement();
String sql = "update new_table set name ='"+user.getName()+"',age = '"+user.getAge()+"' where id = '"+user.getId()+"' ";
PreparedStatement ps = (PreparedStatement)conn.prepareStatement(sql);
i = ps.executeUpdate();
}catch (SQLException e){
System.out.println("__Update Error__");
e.printStackTrace();
}finally {
ps.close();
stmt.close();
}
}
5)带东西的查
public void getById(int id) throws SQLException {
try{
Statement stmt = conn.createStatement();
String sql = "select *from new_table where id = '"+id+"'";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()){
System.out.print(rs.getInt("id") + " ");
System.out.print(rs.getString("name")+" ");
System.out.println(rs.getInt("age"));
}
}catch (SQLException e){
System.out.println("__Get by Id Error__");
e.printStackTrace();
}finally{
ps.close();
stmt.close();
}
}
并且 jdbc 绝对不止这么一点简单的东西。如果熟悉其中的类和方法,可以达到更好的操作体验。
1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
2.建立连接
String url = "jdbc:mysql://localhost:3306/java?user = root&password = root";
Connection conn = DriverManager.getConnection(url, "root", "root");
3.增删改查
1)查:
public void select() throws SQLException {
try {
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM new_table";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.print(rs.getInt(1)+" ");
System.out.print(rs.getString(2)+" ");
System.out.println(rs.getInt(3));
}
} catch (SQLException e) {
System.out.println("__Select Error__");
e.printStackTrace();
}finally {
rs.close();
stmt.close();
}
}
2)删
public void delete(int id) throws SQLException {
try{
Statement stmt = conn.createStatement();
String sql = "DELETE FROM new_table WHERE id =?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1,id);
ps.executeUpdate();
} catch (SQLException e) {
System.out.println("__Delete Error__");
e.printStackTrace();
}finally {
ps.close();
stmt.close();
}
}
3)增
public void insert(User user) throws SQLException {
try{
int i = 0;
Statement stmt = conn.createStatement();
sql = "INSERT INTO new_table(id, name, age) value(?,?,?)";
ps = (PreparedStatement)conn.prepareStatement(sql);
ps.setInt(1, user.getId());
ps.setString(2,user.getName());
ps.setInt(3, user.getAge());
i = ps.executeUpdate();
} catch (SQLException e) {
System.out.println("__Insert Error__");
e.printStackTrace();
}finally {
ps.close();
stmt.close();
}
}
4)改
public void update(User user) throws SQLException{
try{
int i= 0;
Statement stmt = conn.createStatement();
String sql = "update new_table set name ='"+user.getName()+"',age = '"+user.getAge()+"' where id = '"+user.getId()+"' ";
PreparedStatement ps = (PreparedStatement)conn.prepareStatement(sql);
i = ps.executeUpdate();
}catch (SQLException e){
System.out.println("__Update Error__");
e.printStackTrace();
}finally {
ps.close();
stmt.close();
}
}
5)带东西的查
public void getById(int id) throws SQLException {
try{
Statement stmt = conn.createStatement();
String sql = "select *from new_table where id = '"+id+"'";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()){
System.out.print(rs.getInt("id") + " ");
System.out.print(rs.getString("name")+" ");
System.out.println(rs.getInt("age"));
}
}catch (SQLException e){
System.out.println("__Get by Id Error__");
e.printStackTrace();
}finally{
ps.close();
stmt.close();
}
}
相关文章推荐
- jdbc中的Statement和PreparedStatement接口对象
- MySQL中的integer 数据类型
- MySQL存储过程
- Android之获取手机上的图片和视频缩略图thumbnails
- mysql中int、bigint、smallint 和 tinyint的区别与长度
- mysql load data 导出、导入 csv
- source命令执行SQL脚本文件
- linux下mysql添加用户
- mysql procedure
- mysql触发器
- 数据库链接字符串查询网站
- MySQL 备份和恢复策略
- mac下安装mysql(转载)
- mysql 修改编码 Linux/Mac/Unix/通用(杜绝修改后无法启动的情况!)
- MySQL数据的导出、导入(mysql内部命令:mysqldump、mysql)
- mysql数据行转列
- Linux下修改MySQL编码的方法
- MySQL Server 日志