JDBC--Statement(添加)

php中文网
发布: 2016-06-07 15:28:17
原创
1450人浏览过

第一种方法: import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;public class DataInsert {public static void main(String[] args) {Connection con=null;Statement stat=null;try {Clas

第一种方法:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class DataInsert {
	public static void main(String[] args) {
		Connection con=null;
		Statement stat=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url="jdbc:mysql://localhost:3306/db_book";
			con=DriverManager.getConnection(url,"root","123456");
			stat=con.createStatement();
			String sql="insert into t_user(id,userName,password)values(2,'java','123')";
			stat.executeUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				stat.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				con.close();
			} catch (SQLException e) {
		
				e.printStackTrace();
			}
		}
	}

}
登录后复制

运行结果

 

\

第二种方法

 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class DataInsert2 {
	private static void add(int id,String userName,String password)throws Exception{
		Connection con=null;
		Statement stat=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url="jdbc:mysql://localhost:3306/db_book";
			con=DriverManager.getConnection(url,"root","123456");
			stat=con.createStatement();
			String sql="insert into t_user values("+id+",'"+userName+"','"+password+"')";
			stat.executeUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				stat.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}	
	}
	public static void main(String[] args) throws Exception {
	   add(4,"java2","123456");
	}
}
登录后复制
运行结果:

\
 

第三种方法(面向对象):

User

Mokker AI
Mokker AI

AI产品图添加背景

Mokker AI 55
查看详情 Mokker AI
public class User {
	private int id;
	private String userName;
	private String password;

	public User(int id, String userName, String password) {
		super();
		this.id = id;
		this.userName = userName;
		this.password = password;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
登录后复制
public class DataInsert3 {
	private static void add(User user) throws Exception {
		Connection con = null;
		Statement stat = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/db_book";
			con = DriverManager.getConnection(url, "root", "123456");
			stat = con.createStatement();
			String sql = "insert into t_user values(" + user.getId() + ",'"
					+ user.getUserName() + "','" + user.getPassword() + "')";
			stat.executeUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				stat.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) throws Exception {
		User user=new User(5, "java6", "123456");
		add(user);
	}
}
登录后复制

运行结果

\

第四种方法(是不是有的代码写重复了)

DbUtil

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class DbUtil {
	public static Connection getConnection() throws Exception {
		Connection con = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/db_book";
			con = DriverManager.getConnection(url, "root", "123456");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
	}
	public static void close(Connection con,Statement stat){
		try {
			stat.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
登录后复制
import java.sql.Connection;
import java.sql.Statement;

public class DataInsert4 {
	private static void add(User user) throws Exception {
		Connection con = null;
		Statement stat = null;
		try {
			con=DbUtil.getConnection();
			stat = con.createStatement();
			String sql = "insert into t_user values(" + user.getId() + ",'"
					+ user.getUserName() + "','" + user.getPassword() + "')";
			stat.executeUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbUtil.close(con, stat);
		}
	}

	public static void main(String[] args) throws Exception {
		User user=new User(6, "java7", "123456");
		add(user);
	}
}
登录后复制

运行结果:

\

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号