MySQL与JDBC连接
来源:互联网 发布:试点网络学校基础 编辑:程序博客网 时间:2024/04/29 18:38
必须的准备工作
一、MySQL的安装。可以参考博文:http://blog.csdn.net/jueblog/article/details/9499245
二、下载 jdbc 驱动。可以从在官网上下载,或者点击 http://download.csdn.net/detail/oyuntaolianwu/5822697 下载
三、在 Eclipse 的 Java 工程中新建一个 lib 文件夹,将所准备的 JAR 包复制进去。
四、右键该 JAR 包:Build Path --> Add to Build Path
连接的理论知识
需要用到 Java.sql.*; 中的几个相关类:
Connection类
负责建立与指定URL(包含数据库IP地址、库名、用户名和密码的信息)的连接;
Connection conn = DriverManager.getConnection(url,user,password);
DriverManager.getConnection(url);
利用驱动管理器类获取指定URL连接
[java] view plaincopy
String url = "jdbc:mysql://localhost:3306/test"; //连接URL为 jdbc:mysql//服务器地址/数据库名
Statement类
stmt = conn.createStatment();
语句对象,用来向数据库发送一条SQL语句
rs = stmt.executeQuery(sql) //返回记录集对象,用于查询
int count = stmt.executeUpdate(sql) //执行一条增删改语句,返回int
stmt.execute(sql) //增删改都可以,返回布尔值(执行成功or失败)
ResultSet类
记录集对象,存储executeQuery()方法返回的记录集合。用相关的rs.getString("列名") rs.getInt("列名")等方法获取指定列的值。
连接测试
Java代码如下:
[java] view plaincopy
package com.sql;
import java.sql.*;
public class JDBC0726_Base {
Connection connection;
Statement statement;
ResultSet rSet;
//返回一个与特定数据库的连接
public Connection getConnection() {
try {
//连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "yongqiang");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
public static void main(String[] args) {
JDBC0726_Base jDao = new JDBC0726_Base();
System.out.println(jDao.getConnection());
}
}
如果输出相应的对象地址,而不是异常,则证明连接成功。
如输出:com.mysql.jdbc.JDBC4Connection@200bde
用 Java 对SQL进行相关操作
【注】以下操作需要依托于上面的 getConnection() 方法
非查询类SQL语句
[java] view plaincopy
//非查询类
public int Update(String sql) {
getConnection();
int count =0;
try {
statement = connection.createStatement();
count = statement.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if (statement != null) {
statement.close();
statement = null;
}
if (connection != null) {
connection.close();
connection = null;
}
} catch (SQLException e2) {
// TODO: handle exception
}
}
return count;
}
应用:
[java] view plaincopy
System.out.println(jDao.Update("INSERT INTO t_user(username,password,sex) values('hehe','131','n');"));
输出值为 1 则证明添加成功。
查询类 SQL 并返回多条记录
[java] view plaincopy
//执行一条查询类SQL,返回多条记录集
public void Qurty(String sql) {
getConnection();
try {
statement = connection.createStatement();
rSet = statement.executeQuery(sql);
System.out.println("id\t" + "realName\t"+"school\t");
while (rSet.next()) {
System.out.println(rSet.getRow()+ "----" + rSet.getString("id") + "\t" + rSet.getString("realName") + "\t" + rSet.getString("school") + "\t");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if (statement != null) {
statement.close();
statement = null;
}
if (rSet != null) {
rSet.close();
rSet = null;
}
} catch (SQLException e2) {
// TODO: handle exception
}
}
}
应用举例:
[java] view plaincopy
jDao.Qurty("SELECT * FROM t_user WHERE sex='女';");
System.out.println("-----------------------------");
jDao.Qurty("SELECT * FROM t_user;");
System.out.println("-----------------------------");
查询类 SQL 并返回一条记录
[java] view plaincopy
//执行一条查询类SQL,返回单条记录集
public void QurtyByUnique(String sql) {
getConnection();
try {
statement = connection.createStatement();
rSet = statement.executeQuery(sql);
System.out.println("id\t" + "realName\t"+"school\t");
if (rSet.next()) {
System.out.println(rSet.getInt("id") + "\t" + rSet.getString("realName") + "\t" + rSet.getString("school") + "\t");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if (statement != null) {
statement.close();
statement = null;
}
if (rSet != null) {
rSet.close();
rSet = null;
}
} catch (SQLException e2) {
// TODO: handle exception
}
}
}
应用举例:
[java] view plaincopy
jDao.QurtyByUnique("SELECT * FROM t_user WHERE sex='女';");
System.out.println("-----------------------------");
jDao.QurtyByUnique("SELECT * FROM t_user;");
System.out.println("-----------------------------");
输出表单所有数据
[java] view plaincopy
public void QurtyTest(String sql) {
getConnection();
try {
statement = connection.createStatement();
rSet = statement.executeQuery(sql);
while (rSet.next()) {
System.out.print(rSet.getRow()+ "----" );
for (int i = 1; i < 14; i++) {
System.out.print(rSet.getString(i) +"\t");
}
System.out.println();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if (statement != null) {
statement.close();
statement = null;
}
if (rSet != null) {
rSet.close();
rSet = null;
}
} catch (SQLException e2) {
// TODO: handle exception
}
}
}<span style="font-family:Arial,Helvetica,sans-serif"> </span>
一、MySQL的安装。可以参考博文:http://blog.csdn.net/jueblog/article/details/9499245
二、下载 jdbc 驱动。可以从在官网上下载,或者点击 http://download.csdn.net/detail/oyuntaolianwu/5822697 下载
三、在 Eclipse 的 Java 工程中新建一个 lib 文件夹,将所准备的 JAR 包复制进去。
四、右键该 JAR 包:Build Path --> Add to Build Path
连接的理论知识
需要用到 Java.sql.*; 中的几个相关类:
Connection类
负责建立与指定URL(包含数据库IP地址、库名、用户名和密码的信息)的连接;
Connection conn = DriverManager.getConnection(url,user,password);
DriverManager.getConnection(url);
利用驱动管理器类获取指定URL连接
[java] view plaincopy
String url = "jdbc:mysql://localhost:3306/test"; //连接URL为 jdbc:mysql//服务器地址/数据库名
Statement类
stmt = conn.createStatment();
语句对象,用来向数据库发送一条SQL语句
rs = stmt.executeQuery(sql) //返回记录集对象,用于查询
int count = stmt.executeUpdate(sql) //执行一条增删改语句,返回int
stmt.execute(sql) //增删改都可以,返回布尔值(执行成功or失败)
ResultSet类
记录集对象,存储executeQuery()方法返回的记录集合。用相关的rs.getString("列名") rs.getInt("列名")等方法获取指定列的值。
连接测试
Java代码如下:
[java] view plaincopy
package com.sql;
import java.sql.*;
public class JDBC0726_Base {
Connection connection;
Statement statement;
ResultSet rSet;
//返回一个与特定数据库的连接
public Connection getConnection() {
try {
//连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "yongqiang");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
public static void main(String[] args) {
JDBC0726_Base jDao = new JDBC0726_Base();
System.out.println(jDao.getConnection());
}
}
如果输出相应的对象地址,而不是异常,则证明连接成功。
如输出:com.mysql.jdbc.JDBC4Connection@200bde
用 Java 对SQL进行相关操作
【注】以下操作需要依托于上面的 getConnection() 方法
非查询类SQL语句
[java] view plaincopy
//非查询类
public int Update(String sql) {
getConnection();
int count =0;
try {
statement = connection.createStatement();
count = statement.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if (statement != null) {
statement.close();
statement = null;
}
if (connection != null) {
connection.close();
connection = null;
}
} catch (SQLException e2) {
// TODO: handle exception
}
}
return count;
}
应用:
[java] view plaincopy
System.out.println(jDao.Update("INSERT INTO t_user(username,password,sex) values('hehe','131','n');"));
输出值为 1 则证明添加成功。
查询类 SQL 并返回多条记录
[java] view plaincopy
//执行一条查询类SQL,返回多条记录集
public void Qurty(String sql) {
getConnection();
try {
statement = connection.createStatement();
rSet = statement.executeQuery(sql);
System.out.println("id\t" + "realName\t"+"school\t");
while (rSet.next()) {
System.out.println(rSet.getRow()+ "----" + rSet.getString("id") + "\t" + rSet.getString("realName") + "\t" + rSet.getString("school") + "\t");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if (statement != null) {
statement.close();
statement = null;
}
if (rSet != null) {
rSet.close();
rSet = null;
}
} catch (SQLException e2) {
// TODO: handle exception
}
}
}
应用举例:
[java] view plaincopy
jDao.Qurty("SELECT * FROM t_user WHERE sex='女';");
System.out.println("-----------------------------");
jDao.Qurty("SELECT * FROM t_user;");
System.out.println("-----------------------------");
查询类 SQL 并返回一条记录
[java] view plaincopy
//执行一条查询类SQL,返回单条记录集
public void QurtyByUnique(String sql) {
getConnection();
try {
statement = connection.createStatement();
rSet = statement.executeQuery(sql);
System.out.println("id\t" + "realName\t"+"school\t");
if (rSet.next()) {
System.out.println(rSet.getInt("id") + "\t" + rSet.getString("realName") + "\t" + rSet.getString("school") + "\t");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if (statement != null) {
statement.close();
statement = null;
}
if (rSet != null) {
rSet.close();
rSet = null;
}
} catch (SQLException e2) {
// TODO: handle exception
}
}
}
应用举例:
[java] view plaincopy
jDao.QurtyByUnique("SELECT * FROM t_user WHERE sex='女';");
System.out.println("-----------------------------");
jDao.QurtyByUnique("SELECT * FROM t_user;");
System.out.println("-----------------------------");
输出表单所有数据
[java] view plaincopy
public void QurtyTest(String sql) {
getConnection();
try {
statement = connection.createStatement();
rSet = statement.executeQuery(sql);
while (rSet.next()) {
System.out.print(rSet.getRow()+ "----" );
for (int i = 1; i < 14; i++) {
System.out.print(rSet.getString(i) +"\t");
}
System.out.println();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if (statement != null) {
statement.close();
statement = null;
}
if (rSet != null) {
rSet.close();
rSet = null;
}
} catch (SQLException e2) {
// TODO: handle exception
}
}
}<span style="font-family:Arial,Helvetica,sans-serif"> </span>
0 0
- jdbc 与 mysql连接
- MySQL与JDBC连接
- JDBC与mySQL连接
- Jsp与MySQL的JDBC连接
- oracle与mysql的JDBC连接
- jsp通过jdbc驱动与mysql连接
- JDBC 实现 Elipse 与 MySQL 的连接
- java--jdbc与mysql的连接
- JDBC 实现 Elipse 与 MySQL 的连接
- 使用JDBC获取与mysql的连接
- jdbc之mysql连接与操作
- JDBC连接MySQL数据库
- JDBC连接MySQL数据库
- JDBC连接MYSQL
- JDBC连接MySQL
- 使用JDBC连接MySql
- JDBC连接mysql代码
- JDBC连接MySQL
- UITableView或UIScrollView的content截屏
- iOS缓存
- 改变软件的样式风格的一些思路
- android布局中的基本属性:
- 解析几何:第四章 空间中的直线与平面(1)空间直线的方向、平面方程、空间直线方程
- MySQL与JDBC连接
- GVIM配置NerdTree
- Notes of Hinton's lecture
- iOS深复制浅复制
- 安卓绘图之贝塞尔曲线
- Open Source -- 不要为了开源而开源
- iOS 并发:NSOperation 与调度队列入门(1)
- Leet Code OJ 21. Merge Two Sorted Lists [Difficulty: Easy]
- Mysql 存储过程、函数和触发器