Java程序链接MySQL数据库
2015-11-18 10:12
375 查看
JDBC
java链接数据库主要分为以下五个步骤
上述第三步时,我们一般采用预编译PreparedStatement新建对象
代码实例
java链接数据库主要分为以下五个步骤
//1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); //2.创建一个链接对象 Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test1", "root", "root"); //3.创建一个sql语句的发送命令对象 Statement stmt = (Statement) conn.createStatement(); //4.执行sql语句拿到结果集 ResultSet rs = stmt.executeQuery("select * from stu"); while (rs.next()) { System.out.println(rs.getInt("id") + ":" + rs.getString("name")); //根据字段名拿取(推荐) int id = rs.getInt("id");//结果集的字段 不是物理表的字段 String name = rs.getString("name"); //根据字段的索引(索引从1开始) int id = rs.getInt(1); String name = rs.getString(2); System.out.println(id+":"+name); } //5.关闭链接,命令对象以及结果集 rs.close(); stmt.close(); conn.close();
上述第三步时,我们一般采用预编译PreparedStatement新建对象
PreparedStatement ps = (PreparedStatement)conn .prepareStatement( "select*from people where name =? and password =?"); ps.setString(1, name); ps.setString(2, password); //查询语句返回的是一个结果集 ResultSet rs = ps.executeQuery(); //更新语句返回整形,成功为1,失败为0 ps.executeUpdate();
代码实例
public class DBUtil { /** * 1:connection :负责java程序与数据库之间的链接 2:Statement:负责执行SQL语句 3;Resultset:存放结果集 * */ // 链接数据库 public static Connection getConnection() { Connection conn = null;// 负责JAVA程序与数据库程序之间的链接 try { Class.forName("com.mysql.jdbc.Driver");// 加载mysql数据库驱动 死记硬背必须掌握 // 链接数据库:根据数据库的URL 用户名 密码 conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test1", "root", "123456");// test1是数据库的名字,根据自己数据库的名字更改 } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace();// 打印异常信息 } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; public static String QueryUser(String name, String password) { Connection conn = getConnection();// 获取数据库的链接 try { PreparedStatement ps = (PreparedStatement) conn .prepareStatement("select *from people where name =? and password =?"); ps.setString(1, name); ps.setString(2, password); //执行SQL语句 ResultSet rs = ps.executeQuery(); if(rs.next()){//指向下一行,如果有数据返回true,没有返回false return "true"; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "false"; } public static void InsertUser(String name,String password){ Connection conn = getConnection(); PreparedStatement ps; try { ps = (PreparedStatement) conn .prepareStatement("insert into people(name,password) values(?,?)"); ps.setString(1, name); ps.setString(2, password); ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
相关文章推荐
- MySQL中的integer 数据类型
- MySQL存储过程
- Android之获取手机上的图片和视频缩略图thumbnails
- mysql中int、bigint、smallint 和 tinyint的区别与长度
- mysql load data 导出、导入 csv
- source命令执行SQL脚本文件
- MySQL创建用户及权限控制
- MySQL管理数据表
- linux下mysql添加用户
- mysql procedure
- mysql触发器
- 数据库链接字符串查询网站
- MySQL 备份和恢复策略
- mac下安装mysql(转载)
- mysql 修改编码 Linux/Mac/Unix/通用(杜绝修改后无法启动的情况!)
- MySQL数据的导出、导入(mysql内部命令:mysqldump、mysql)
- mysql数据行转列
- Linux下修改MySQL编码的方法