连接数据库
//================================================= // File Name : MySQL_demo //------------------------------------------------------------------------------ // Author : Common import java.sql.*; //主类 //Function : MySQL_demo public class MySQL_demo { //定义MySQL的数据库驱动程序 public static final String DBDRIVER = "org.gjt.mm.mysql.Driver"; //定义MySQL数据库的连接地址 public static final String DBURL = "jdbc:mysql://localhost:3306/mysql_demo"; //MySQL数据库的连接用户名 public static final String DBUSER = "root"; //MySQL数据库的连接密码 public static final String DBPASS = "123456"; public static void main(String[] args) { // TODO 自动生成的方法存根 Connection conn = null; //数据库连接 try{ Class.forName(DBDRIVER); //加载MYSQL JDBC驱动程序 }catch(ClassNotFoundException e){ e.printStackTrace(); } try{ //连接MySQL数据库时,要写上连接的用户名和密码 conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS); }catch(SQLException e){ e.printStackTrace(); } System.out.println(conn); try{ conn.close(); //数据库关闭 }catch(SQLException e){ e.printStackTrace(); } } }
建立一个user表
CREATE TABLE user(id INT PRIMARY KEY AUTO_INCREMENT,name VARCHAR(30) NOT NULL,password VARCHAR(32) NOT NULL,age INT NOT NULL,sex VARCHAR(2),birthday DATE);
<1>数据库的更新操作
执行数据库插入操作,执行一次插入一条
//================================================= // File Name : MySQL_demo //------------------------------------------------------------------------------ // Author : Common import java.sql.*; //主类 //Function : MySQL_demo public class MySQL_demo { //定义MySQL的数据库驱动程序 public static final String DBDRIVER = "org.gjt.mm.mysql.Driver"; //定义MySQL数据库的连接地址 public static final String DBURL = "jdbc:mysql://localhost:3306/mysql_demo"; //MySQL数据库的连接用户名 public static final String DBUSER = "root"; //MySQL数据库的连接密码 public static final String DBPASS = "123456"; public static void main(String[] args) throws Exception { // TODO 自动生成的方法存根 Connection conn = null; //数据库连接 Statement stmt = null; //数据库操作 String sql = "INSERT INTO user(name,password,age,sex,birthday)" +"VALUES('张三','mima',30,'男','2014-01-11')"; Class.forName(DBDRIVER); //加载驱动程序 //连接MySQL数据库时,要写上连接的用户名和密码 conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS); stmt = conn.createStatement(); //实例化Statement对象 stmt.executeUpdate(sql); //执行数据库更新操作 stmt.close(); //操作关闭 conn.close(); //数据库关闭 } }
//================================================= // File Name : MySQL_demo //------------------------------------------------------------------------------ // Author : Common import java.sql.*; //主类 //Function : MySQL_demo public class MySQL_demo { //定义MySQL的数据库驱动程序 public static final String DBDRIVER = "org.gjt.mm.mysql.Driver"; //定义MySQL数据库的连接地址 public static final String DBURL = "jdbc:mysql://localhost:3306/mysql_demo"; //MySQL数据库的连接用户名 public static final String DBUSER = "root"; //MySQL数据库的连接密码 public static final String DBPASS = "123456"; public static void main(String[] args) throws Exception { // TODO 自动生成的方法存根 Connection conn = null; //数据库连接 Statement stmt = null; //数据库操作 String name = "李四"; String password = "pwd"; int age = 22; String sex = "女"; String birthday = "2012-01-01"; String sql = "INSERT INTO user(name,password,age,sex,birthday)" +"VALUES('"+name+"','"+password+"',"+age+",'"+sex+"','"+birthday+"')"; Class.forName(DBDRIVER); //加载驱动程序 //连接MySQL数据库时,要写上连接的用户名和密码 conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS); stmt = conn.createStatement(); //实例化Statement对象 stmt.executeUpdate(sql); //执行数据库更新操作 stmt.close(); //操作关闭 conn.close(); //数据库关闭 } }
<2>执行数据库的修改
//================================================= // File Name : MySQL_demo //------------------------------------------------------------------------------ // Author : Common import java.sql.*; //主类 //Function : MySQL_demo public class MySQL_demo { //定义MySQL的数据库驱动程序 public static final String DBDRIVER = "org.gjt.mm.mysql.Driver"; //定义MySQL数据库的连接地址 public static final String DBURL = "jdbc:mysql://localhost:3306/mysql_demo"; //MySQL数据库的连接用户名 public static final String DBUSER = "root"; //MySQL数据库的连接密码 public static final String DBPASS = "123456"; public static void main(String[] args) throws Exception { // TODO 自动生成的方法存根 Connection conn = null; //数据库连接 Statement stmt = null; //数据库操作 int id = 2; String name = "王五"; String password = "pwd2"; int age = 25; String sex = "女"; String birthday = "2002-11-21"; String sql = "UPDATE user SET name= '"+name+"' , password='"+password+"' , age="+age+" , sex='"+sex+"' , birthday='"+birthday+"' WHERE id="+id; Class.forName(DBDRIVER); //加载驱动程序 //连接MySQL数据库时,要写上连接的用户名和密码 conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS); stmt = conn.createStatement(); //实例化Statement对象 stmt.executeUpdate(sql); //执行数据库更新操作 stmt.close(); //操作关闭 conn.close(); //数据库关闭 } }
<3>执行数据库删除操作
//================================================= // File Name : MySQL_demo //------------------------------------------------------------------------------ // Author : Common import java.sql.*; //主类 //Function : MySQL_demo public class MySQL_demo { //定义MySQL的数据库驱动程序 public static final String DBDRIVER = "org.gjt.mm.mysql.Driver"; //定义MySQL数据库的连接地址 public static final String DBURL = "jdbc:mysql://localhost:3306/mysql_demo"; //MySQL数据库的连接用户名 public static final String DBUSER = "root"; //MySQL数据库的连接密码 public static final String DBPASS = "123456"; public static void main(String[] args) throws Exception { // TODO 自动生成的方法存根 Connection conn = null; //数据库连接 Statement stmt = null; //数据库操作 int id = 3; String sql = "DELETE FROM user WHERE id=" + id; Class.forName(DBDRIVER); //加载驱动程序 //连接MySQL数据库时,要写上连接的用户名和密码 conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS); stmt = conn.createStatement(); //实例化Statement对象 stmt.executeUpdate(sql); //执行数据库更新操作 stmt.close(); //操作关闭 conn.close(); //数据库关闭 } }
在JDK1.7中,java.sql中的Connection,Statement,ResultSet接口都继承了AutoCloseable,所以使用了try-with-resource
可以参考:java try-with-resource语句使用