一 、 JDBC的开发步骤

1、准备四大参数

2、注册驱动

3、获得连接

4、获得语句执行者

5、执行sql语句

6、处理结果

7、释放资源

1、准备四大参数

/*
* jdbc四大配置参数
* > driverClassName: java.mysql.jdbc.Driver --驱动类名
* > url:jdbc:mysql://localhost:3306/数据库名
* > username:root
* > password:
* */ string driverClassName="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/sine";
String username="root";
String password ="";

2、注册驱动

Class.forName("com.mysql.jdbc.Driver");//加载驱动类

实际上等效于 ==

DriverManager.registerDriver(new com.mysql.jdbc.Driver());

因为在com.mysql.jdbc.Driver的实现类中,有一个静态代码块,即加载类的时候,会执行 静态代码块的代码。

3、获得连接

//连接对象
Connection conn = (Connection) DriverManager.getConnection(url, user, password);

4、获得语句执行者

/*
* 通过Connection对象创建Statement
* > Statement语句发送器,它的功能就是向数据库发送sql语句
**/
Statement stmt = conn.createStatement();

5、执行sql语句

//  调用它的int executeUpdate(String sql),它可以执行DML、DDL

// 插入
//String sql = "insert into user values('10','eager','1999-09-09','1000','smile')";
//int r = stmt.executeUpdate(sql); // 查询
String sql = "select * from user";
ResultSet rs = st.executeQuery(sql); 在ResultSet类中提供一系列的getXXX()方法, 比较常用的方法有:
Object getObject(int col)/Object getObject(String colName),获得任意对象
String getString(int col),获得字符串
int getInt(int col),获得整形
double getDouble(int col),获得双精度浮点行

6、处理结果

while(rs.next()){
int uid = rs.getInt("uid");
String uname = rs.getString("uname"); System.out.println( uid + uname );
}

7、释放资源

//与IO 流一样,使用后的东西都要关闭!关闭的顺序是先得到的后关闭,后得到的先关闭。

rs.close();
stmt.close();
conn.close();

Demo

public static Connection getConnection() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/mydb1";
return DriverManager.getConnection(url, "root", "123");
} @Test
public void query() {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = getConnection();
stmt = con.createStatement();
String sql = "select * from user";
rs = stmt.executeQuery(sql);
while(rs.next()) {
String username = rs.getString(1);
String password = rs.getString(2);
System.out.println(username + ", " + password);
}
} catch(Exception e) {
throw new RuntimeException(e);
} finally {
try {
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(con != null) con.close();
} catch(SQLException e) {}
}
}

MySQL -- JDBC的更多相关文章

  1. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'dd' in 'where clause'

    今天在使用mysql数据库查找数据的时候报错,错误信息如下: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown co ...

  2. Class.forName("com.mysql.jdbc.Driver") ;

    try { Class.forName("com.mysql.jdbc.Driver") ; } catch(ClassNotFoundException e) { System. ...

  3. [bigdata] 启动CM出现 “JDBC Driver class not found: com.mysql.jdbc.Driver” 以及“Error creating bean with name 'serverLogFetcherImpl'”问题的解决方法

    问题:“JDBC Driver class not found: com.mysql.jdbc.Driver”  通过以下命令启动cm [root@hadoop1 ~]# /etc/init.d/cl ...

  4. java中myeclipse连接mysql问题(java.lang.ClassNotFoundException: com.mysql.jdbc.Driver)

    java中myeclipse连接mysql问题(java.lang.ClassNotFoundException: com.mysql.jdbc.Driver) 1.往项目中添加mysql-conne ...

  5. MySQL JDBC/MyBatis Stream方式读取SELECT超大结果集

    情景: 遍历并处理一个大表中的所有数据, 这个表中的数据可能会是千万条或者上亿条, 很多人可能会说用分页limit……但需求本身一次性遍历更加方便, 且Oracle/DB2都有方便的游标机制. 对DB ...

  6. MySQL JDBC 出现多个 SHOW VARIABLES 语句。

    一次偶然的机会,show processlist 的时候,发现有个 Client 一直在执行  "mysql-connector-java-5.1.21 ( Revision: ${bzr. ...

  7. Type mismatch: cannot convert from java.sql.PreparedStatement to com.mysql.jdbc.PreparedStatement

    Connection.prepareStatement()函数出错,提示: Type mismatch: cannot convert from java.sql.PreparedStatement ...

  8. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 解决办法

    09:00:30.307 [http-8080-6] ERROR org.hibernate.transaction.JDBCTransaction -JDBC begin failed com.my ...

  9. 【转】关于Class.forName(“com.mysql.jdbc.Driver”)

    原文:http://www.cnblogs.com/gaojing/archive/2012/03/23/2413638.html 传统的使用jdbc来访问数据库的流程为: Class.forName ...

  10. mysql 插入中文报错: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value...

    总结写在前面, 总结: 当Java通过jdbc链接mysql插入中文时,要保证程序可以正常执行,而且插入的中文不会乱码, mysql服务器端,对数据表(不是数据库)的编码设置,要保证是支持中文的,例如 ...

随机推荐

  1. 【bzoj1026】 SCOI2009—windy数

    http://www.lydsy.com/JudgeOnline/problem.php?id=1026 (题目链接) 题意 在区间${[A,B]}$有多少个数相邻两个数位上的数之差至少为2. Sol ...

  2. linux内核分析 第八周读书笔记

    第四章 进程调度 4.1 多任务 1.多任务操作系统就是能同时并发的交互执行多个进程的操作系统. 2.多任务操作系统使多个进程处于堵塞或者睡眠状态,实际不被投入执行,这些任务尽管位于内存,但是并不处于 ...

  3. 《Linux内核设计与实现》学习总结 Chap3

    第三章 进程管理 进程是Unix操作系统抽象概念中最基本的一种.我们拥有操作系统就是为了运行用户程序,因此,进程管理就是所有操作系统的心脏所在. 3.1进程 概念: 进程:处于执行期的程序.但不仅局限 ...

  4. bzoj3672: [Noi2014]购票(树形DP+斜率优化+可持久化凸包)

    这题的加强版,多了一个$l_i$的限制,少了一个$p_i$的单调性,难了好多... 首先有方程$f(i)=min\{f(j)+(dep_i-dep_j)*p_i+q_i\}$ $\frac {f(j) ...

  5. 解题:HEOI 2016 求和

    题面 我们需要知道这样一个东西(大概叫 斯特林公式?) $S(i,j)=\frac{1}{j!}\sum\limits_{k=0}^{j}(-1)^k C_j^k(j-k)^i$ 那么就是推啊 $=\ ...

  6. E. Turn Off The TV Educational Codeforces Round 29

    http://codeforces.com/contest/863/problem/E 注意细节 #include <cstdio> #include <cstdlib> #i ...

  7. jsp中的js中获取项目路径的方法

    在jsp中加上 <% String path = request.getContextPath(); String basePath = request.getScheme()+":/ ...

  8. bzoj千题计划150:bzoj2738: 矩阵乘法

    http://www.lydsy.com/JudgeOnline/problem.php?id=2738 整体二分 二维树状数组累积 #include<cstdio> #include&l ...

  9. openGL笔记-画基本图形

    #include "iostream" #include <GL/glut.h> #include<cmath> #include<vector> ...

  10. android 水波纹效果实现

    1.在drawable文件下,新建seletor,作为button的背景,这里我用的是两个圆角的shape <?xml version="1.0" encoding=&quo ...