第一步,需要下载JDBC驱动, 点我。然后选择合适的版本即可。

下载完成之后解压,然后将mysql-connector-java-5.1.6-bin.jar文件放到java的安装目录下面。

这里每个人的安装路径不同,需要找到自己的路径。

第二步,需要在mysql下新建一个数据库,这个很简单,比如create database test; 

然后use test;来切换当前正在使用的数据库。

还需要新建一张表,因为下面我们的一些操作是在表上进行的。

第三步,加载JDBC驱动,连接数据库。

下面代码仅供参考,在我的机器上可以执行。

 import java.sql.*;
import java.util.jar.JarException; public class DBManager {
// 用户名
private String user = "";
// 密码
private String password = "";
// 主机
private String host = "";
// 数据库名字
private String database = "";
/*
*
* private String
* url="jdbc:mysql://"+host+"/"+"useUnicode=true&characterEncoding=GB2312";
*/
private String url = "";
private Connection con = null;
Statement stmt;
/**
*
* 根据主机、数据库名称、数据库用户名、数据库用户密码取得连接。
*
* @param host
* String
*
* @param database
* String
*
* @param user
* String
*
* @param password
* String
*/
public DBManager(String host, String database, String user, String password) {
this.host = host;
this.database = database;
this.user = user;
this.password = password;
// 显示中文
this.url = "jdbc:mysql://" + host + "/" + database;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.err.println("class not found:" + e.getMessage());
}
try {
con = DriverManager.getConnection(this.url, this.user,
this.password);
// 连接类型为ResultSet.TYPE_SCROLL_INSENSITIVE,
// ResultSet.CONCUR_READ_ONLY
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
} catch (SQLException a) {
System.err.println("sql exception:" + a.getMessage());
}
}
/**
*
* 返回取得的连接
*/
public Connection getCon() {
return con;
}
/**
*
* 执行一条简单的查询语句
*
* 返回取得的结果集
*/
public ResultSet executeQuery(String sql) {
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
/**
*
* 执行一条简单的更新语句
*
* 执行成功则返回true
*/
public boolean executeUpdate(String sql) {
boolean v = false;
try {
v = stmt.executeUpdate(sql) > 0 ? true : false;
} catch (SQLException e) {
e.printStackTrace();
} finally {
return v;
}
}
public static void main(String[] args) throws java.lang.NullPointerException{
ResultSet rs;
DBManager exe = new DBManager("127.0.0.1", "test", "root", "118118"); rs = exe.executeQuery("SELECT * FROM student"); try {
ResultSetMetaData meta_data = rs.getMetaData();//列名
for (int i_col = 1; i_col <= meta_data.getColumnCount(); i_col++) {
System.out.print(meta_data.getColumnLabel(i_col) + " ");
}
System.out.println();
while (rs.next()) {
for (int i_col = 1; i_col <= meta_data.getColumnCount(); i_col++) {
System.out.print(rs.getString(i_col) + " ");
}
System.out.println();
}
} catch (Exception e) { }
}
}

最新文章

  1. Go语言 获取get、post参数
  2. z-index 可以使用负值,CSS相对定位、绝对定位利器
  3. wininet异步InternetReadFile和超时相关问题
  4. 字典转换成NSString(NSJson)
  5. adding validation annotators to model classes 在linq to EntityFrame的Model中添加前台验证validation annotators
  6. python3抓取异步百度瀑布流动态图片(二)get、json下载代码讲解
  7. 刷机总结(阿里云os--&gt;android4.2.2)注明:本文不是教程
  8. android123 zhihuibeijing 新闻中心-新闻 页签 ViewPagerIndicator实现
  9. mac nodejs&amp;npm 安装
  10. 介绍一款开源的类Excel电子表格软件
  11. Windows中通过命令行新建文件夹、新建文件,和一些常用命令
  12. chrome打开收藏夹的网站在新的标签页
  13. 基于mindwave脑电波进行疲劳检测算法的设计(2)
  14. Python爬取猫眼top100排行榜数据【含多线程】
  15. Commons FileUpload文件上传组件
  16. the server ssl certificate failed to verify
  17. 微信小程序-textarea中的文本读取以及换行问题
  18. IEEEXtreme 10.0 - Dog Walking
  19. 执行Java脚本firefox启动成功,不运行test方法,且提示NullPointerException
  20. C# JackLib系列之字体使用

热门文章

  1. 暴力剪枝——cf1181C
  2. HTML5能取代Android和iOS应用程序吗?
  3. linux操作练习题
  4. 利用VS2015自带的报表制作报表
  5. 解决VirtualBox下关于CentOS7网络配置问题
  6. 13_springmvc拦截器应用
  7. svg path命令
  8. C++自己用模板减少工作量
  9. 2018-8-10-使用-ahk-让普通键盘变为Dvorak键盘
  10. 创建你的 /proc 文件