java,jdbc连接mysql
2016-05-01 21:51
375 查看
1.下载jdbc的驱动jar包
2.数据库模样:
![](https://img-blog.csdn.net/20160501215243174?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
3.上代码:
public class JdbcTest {
// 驱动名称
public static final String driverName = "com.mysql.jdbc.Driver";
// 连接地址
public static final String url = "jdbc:mysql://localhost:3306/libo_shopping";
// 用户名
public static final String userName = "root";
// 密码
public static final String password = "root";
// 数据库连接
public static Connection conn = null;
// sql语句执行对象
public static PreparedStatement st = null;
// 结果集对象
public static ResultSet rs = null;
/**
* 获取数据库连接的方法
*
* @return
*/
public static void getConnection() {
try {
// 1.加载驱动
Class.forName(driverName);
// 2.获取连接
conn = DriverManager.getConnection(url, userName, password);
System.out.println("成功获取连接!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 关闭连接的方法
*/
public static void close() {
try {
if(rs!=null){
rs.close();
}
if(st!=null){
st.close();
}
if(conn!=null){
conn.close();
}
System.out.println("关闭成功!!");
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* main方法
* @param args
*/
public static void main(String[] args) {
//1.获取连接
getConnection();
String sql="select * from libo_shopping.y_user";
//2.获取sql语句执行对象
try {
//通过预处理防止sql注入
st=conn.prepareStatement(sql);
//3.获取结果集对象
rs=st.executeQuery();
//4.对结果集进行处理
System.out.println("id\t登录名");
while(rs.next()){
System.out.println(rs.getInt(1)+"\t"+rs.getString(2));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
close();
}
}
}
2.数据库模样:
3.上代码:
public class JdbcTest {
// 驱动名称
public static final String driverName = "com.mysql.jdbc.Driver";
// 连接地址
public static final String url = "jdbc:mysql://localhost:3306/libo_shopping";
// 用户名
public static final String userName = "root";
// 密码
public static final String password = "root";
// 数据库连接
public static Connection conn = null;
// sql语句执行对象
public static PreparedStatement st = null;
// 结果集对象
public static ResultSet rs = null;
/**
* 获取数据库连接的方法
*
* @return
*/
public static void getConnection() {
try {
// 1.加载驱动
Class.forName(driverName);
// 2.获取连接
conn = DriverManager.getConnection(url, userName, password);
System.out.println("成功获取连接!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 关闭连接的方法
*/
public static void close() {
try {
if(rs!=null){
rs.close();
}
if(st!=null){
st.close();
}
if(conn!=null){
conn.close();
}
System.out.println("关闭成功!!");
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* main方法
* @param args
*/
public static void main(String[] args) {
//1.获取连接
getConnection();
String sql="select * from libo_shopping.y_user";
//2.获取sql语句执行对象
try {
//通过预处理防止sql注入
st=conn.prepareStatement(sql);
//3.获取结果集对象
rs=st.executeQuery();
//4.对结果集进行处理
System.out.println("id\t登录名");
while(rs.next()){
System.out.println(rs.getInt(1)+"\t"+rs.getString(2));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
close();
}
}
}
相关文章推荐
- jdbc中的Statement和PreparedStatement接口对象
- MySQL中的integer 数据类型
- MySQL存储过程
- 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编码的方法
- MySQL Server 日志
- MySQL 安全事宜