jdbc
eclipse连接mysql
1.首先下载jar包
链接
http://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.11
然后导入包即可,不用创建设bin什么的
提醒好多小朋友会因为url出问题,根据不同的mysql版本设置不同的url
我的mysql版本是8.0的
代码块
package 数据库;
import java.sql.*;
public class Testconnection1 {
public static void main(String[] args) {
// 声明Connection对象
Connection con = null;
// 设置驱动程序名
String driver = "com.mysql.cj.jdbc.Driver";
// 设置要访问的数据库名
String url="jdbc:mysql://localhost:3306/girls?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL";
// 设置MySQL用户名
String username = "root";
// 设置MySQL密码
String password = "123456789";
try {
// 1. 加载驱动程序
Class.forName(driver);
// 2. 连接MySQL数据库
con = DriverManager.getConnection(url,username,password);
// 3. 创建statement类对象,用来执行SQL语句
Statement statement = con.createStatement();
/*一、删除操作
String sql ="delete from beauty where id=9";
int update =statement.executeUpdate(sql);
if (update>0) {
System.out.println("sucssess");
}else {
System.out.println("failure");
}
//二、修改数据
String sql="update beauty set name='张飞' where id=7";
int update =statement.executeUpdate(sql);
if (update>0) {
System.out.println("sucssess");
}else {
System.out.println("failure");
}
//三、增加
String sql="insert into beauty values(null,'关羽','男','1998-1-1','1161',null,3)";
int update =statement.executeUpdate(sql);
if (update>0) {
System.out.println("sucssess");
}else {
System.out.println("failure");
}
*/
// 4. 查看
String sql = "select * from beauty";
// 5. 执行SQL语句并创建ResultSet类对象存放获取的结果集
ResultSet rs = statement.executeQuery(sql);
// 4.输出结果
System.out.println("-----------------");
String name = null;
int id = 0;
while(rs.next()){
//获取name这列数据
id=rs.getInt("id");
name = rs.getString("name");
//获取address这列数据
//输出结果
System.out.format("%-5d %-20s\n", id, name);
}
System.out.println("-----------------");
} catch (Exception e) {
e.printStackTrace();
}finally{
// 关闭连接
try {
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
接下来介绍下常见问题
- 常见问题
- url根据不同版本会有不同的调整,8.0以上的连接需要用"com.mysql.cj.jdbc.Driver而5.0左右的需要使用"com.mysql.jdbc.Driver
方法二
此类方法更加简介 首先创建一个properties文件吗,为了路径使用方便将其放在src同级目录里
接下来看properties内容
user=root
password=123456789
url=jdbc:mysql://localhost:3306/girls?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
driver=com.mysql.cj.jdbc.Driver
根据不同使用在url添加模板 样例&zeroDateTimeBehavior=CONVERT_TO_NULL&rewriteBatchedStatements=true
接着创建一个java项目使用以下代码块
代码块
package 数据库;
import java.sql.*;
import java.util.Properties;
import java.io.FileInputStream;
public class Testconnection2 {
public static void main(String[] args) throws Exception{
Properties info=new Properties();
info.load(new FileInputStream("src\\myjdbc.properties"));
String user= info.getProperty("user");
String password=info.getProperty("password");
String driver=info.getProperty("driver");
String url =info.getProperty("url");
// TODO 自动生成的方法存根
//1.注册驱动
Class.forName(driver);
//2.获取连接
DriverManager.getConnection(url,user,password);
System.out.println("sucssess");
}
}
我们不得不说以上代码也挺麻烦的,我们可以直接将Connection封装在一个类里直接调用,这样更能体现java封装的思想
代码如下
package 数据库;
import java.io.FileInputStream;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
static String user;
static String password;
static String driver;
static String url;
static{
try{Properties info=new Properties();
info.load(new FileInputStream("src\\myjdbc.properties"));
user= info.getProperty("user");
password=info.getProperty("password");
driver=info.getProperty("driver");
url =info.getProperty("url");
//1.注册驱动
Class.forName(driver);}
catch(Exception e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection(){
try {
return DriverManager.getConnection(url,user,password);
}catch(Exception e) {
throw new RuntimeException(e);
}
}
public static void close(ResultSet set,Statement statement,Connection connection){
try {
if(set!=null) {
set.close();
}
if(statement!=null) {
statement.close();
}
if(connection!=null) {
connection.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
当你需要使用的时候可以直接调用即可