JDBC连接mysql<PreparedStatement>
来源:互联网 发布:买淘宝号 编辑:程序博客网 时间:2024/06/05 20:28
package com.buaa.demo1;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class Test01 { public static void main(String[] args) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost/shopping?characterEncoding=utf8"; String username = "root"; String password = "zhen5296"; String sql = "select * from person"; try{ Class.forName(driver); conn = DriverManager.getConnection(url,username,password); ps = conn.prepareStatement(sql); rs = ps.executeQuery(); Person person = new Person(); while(rs.next()){ person.setId(rs.getInt(1)); person.setName(rs.getString(2)); person.setAge(rs.getInt(3)); person.setPhoto(rs.getString(4)); System.out.println(person.toString()); } }catch(Exception e){ e.printStackTrace(); }finally{ if(rs != null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(ps != null){ try { ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(conn != null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }}
1 0
- JDBC连接mysql<PreparedStatement>
- jdbc连接mysql:statement和preparedStatement的区别
- JDBC 连接数据库 PreparedStatement的使用
- JDBC连接数据库及PreparedStatement详解
- at com.mysql.jdbc.PreparedStatement.asSql
- JDBC-PreparedStatement
- JDBC---PreparedStatement
- jdbc PreparedStatement
- JDBC:PreparedStatement
- com.mysql.jdbc.PreparedStatement和com.mysql.jdbc.PreparedStatement的关系
- 连接Mysql数据库,使用PreparedStatement 语句
- MySQL PreparedStatement 连接数据库抛 MySQLSyntaxErrorException
- JDBC连接数据库 and PreparedStatement和Statement区别
- java(JDBC连接数据库)[对PreparedStatement进行封装]
- JDBC连接MySQL数据库
- JDBC连接MySQL数据库
- JDBC连接MYSQL
- JDBC连接MySQL
- 内核初始化kernel.asm
- Redis数据库的使用场景介绍(避免误用Redis)
- 怎么判断mp3/音频文件加载完成
- Item 47:使用Traits类提供类型信息
- Item 48:了解模板元编程
- JDBC连接mysql<PreparedStatement>
- mysql启动报错:Another MySQL daemon already running with the same unix socket.
- C# 中DateTime的各种使用
- Item 49:new handler的行为
- CentOS取消屏幕保护自动锁屏功能
- PL/SQL教程之五:异常错误处理
- 谷歌设计师的Material Design实践心得
- 动态规划之01背包问题(最易理解的讲解)
- symfony对于mysql自动设置时间字段