java连接mysql5.1教程(含代码)+ 查询数据
下面是数据添加片段的代码
Connection conn = null;
Statement stmt = null;
PreparedStatement ps=null;try{// 注册 JDBC 驱动Class.forName(JDBC_DRIVER);// 打开链接//连接数据库conn = DriverManager.getConnection(DB_URL,USER,PASS);// 执行查询//实例化Statement对象stmt = conn.createStatement();String sql;sql="INSERT INTO login VALUES(?,?)";//向login表里添加数据//注:几列数据几个问号,我的login表只有两列,所以是(?,?)ps=conn.prepareStatement(sql);//添加数据预处理ps.setString(1, "5433");//第1个问号的值ps.setString(2, "214544");//第2个问号的值ps.executeUpdate();//执行添加数据// 完成后关闭ps.close();stmt.close();conn.close();}catch(SQLException se){// 处理 JDBC 错误se.printStackTrace();}catch(Exception e){// 处理 Class.forName 错误e.printStackTrace();}finally{// 关闭资源try{if(stmt!=null) stmt.close();}catch(SQLException se2){}// 什么都不做try{if(conn!=null) conn.close();}catch(SQLException se){se.printStackTrace();}}System.out.println("数据添加成功");
下面是添加数据的完整代码
(为了体现出添加数据的效果,在添加数据前后添加了查询数据的代码)
import java.sql.*;
public class example1 {//MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL //数据库:message Host Address:localhost 端口:3306//请根据实际数据库的信息进行修改static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost:3306/message";// 数据库的用户名与密码//用户名:root 密码:123456//请根据实际数据库的信息进行修改static final String USER = "root";static final String PASS = "123456";public static void main(String[] args) {// TODO Auto-generated method stubConnection conn = null;Statement stmt = null;PreparedStatement ps=null;try{Class.forName(JDBC_DRIVER);conn = DriverManager.getConnection(DB_URL,USER,PASS);stmt = conn.createStatement();String sql;sql = "SELECT * FROM login";//搜索login表ResultSet rs = stmt.executeQuery(sql);while(rs.next()){int id = rs.getInt("id");//得到“id”列的值String password = rs.getString("password");//得到“password”列的值System.out.print("ID: " + id);System.out.print(", 密码: " + password); System.out.print("\n");}rs.close();stmt.close();conn.close();}catch(SQLException se){se.printStackTrace();}catch(Exception e){e.printStackTrace();}finally{try{if(stmt!=null) stmt.close();}catch(SQLException se2){}try{if(conn!=null) conn.close();}catch(SQLException se){se.printStackTrace();}}System.out.print("搜索完毕\n");//开始添加数据try{// 注册 JDBC 驱动Class.forName(JDBC_DRIVER);// 打开链接//连接数据库conn = DriverManager.getConnection(DB_URL,USER,PASS);// 执行查询//实例化Statement对象stmt = conn.createStatement();String sql;sql="INSERT INTO login VALUES(?,?)";//向login表里添加数据//注:几列数据几个问号,我的login表只有两列,所以是(?,?)ps=conn.prepareStatement(sql);//添加数据预处理ps.setString(1, "5433");//第1个问号的值"5433"ps.setString(2, "214544");//第2个问号的值"214544"ps.executeUpdate();//执行添加数据// 完成后关闭ps.close();stmt.close();conn.close();}catch(SQLException se){// 处理 JDBC 错误se.printStackTrace();}catch(Exception e){// 处理 Class.forName 错误e.printStackTrace();}finally{// 关闭资源try{if(stmt!=null) stmt.close();}catch(SQLException se2){}// 什么都不做try{if(conn!=null) conn.close();}catch(SQLException se){se.printStackTrace();}}System.out.println("数据添加成功");//添加数据结束try{Class.forName(JDBC_DRIVER);conn = DriverManager.getConnection(DB_URL,USER,PASS);stmt = conn.createStatement();String sql;sql = "SELECT * FROM login";//搜索login表ResultSet rs = stmt.executeQuery(sql);while(rs.next()){int id = rs.getInt("id");//得到“id”列的值String password = rs.getString("password");//得到“password”列的值System.out.print("ID: " + id);System.out.print(", 密码: " + password); System.out.print("\n");}rs.close();stmt.close();conn.close();}catch(SQLException se){se.printStackTrace();}catch(Exception e){e.printStackTrace();}finally{try{if(stmt!=null) stmt.close();}catch(SQLException se2){}try{if(conn!=null) conn.close();}catch(SQLException se){se.printStackTrace();}}System.out.print("搜索完毕\n");}
}
运行后的效果截图:
(往数据库中添加了数据 ID:5433 密码:214544)
通过java往mysql中添加数据的操作就完成了
如有错误
欢迎指出
下一篇:java对mysql的简单操作——删除数据
总结篇:java对mysql的简单操作——增删改查的总结