Home > Database > Mysql Tutorial > JDBC--Statement(添加)

JDBC--Statement(添加)

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 15:28:17
Original
1135 people have browsed it

第一种方法: 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();
			}
		}
	}

}
Copy after login

运行结果

\

第二种方法

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");
	}
}
Copy after login
运行结果:

\

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

User

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;
	}

}
Copy after login
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);
	}
}
Copy after login

运行结果

\

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

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();
		}
	}
}
Copy after login
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);
	}
}
Copy after login

运行结果:

\

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
MySQL multi-expression if statement
From 1970-01-01 08:00:00
0
0
0
flow control statement
From 1970-01-01 08:00:00
0
0
0
php - What is wrong with this sql statement?
From 1970-01-01 08:00:00
0
0
0
list statement problem
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template