创建一个Java工程
image.png image.png image.png image.png image.png image.png image.pngimage.png image.png image.png
mysql连接测试
image.png image.png image.png image.png为工程导入mysql驱动
image.png image.png image.png image.png测试代码
连接mysql数据库,创建一张表
package com.knight.demo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcDemo {
//url
private static String url = "jdbc:mysql://localhost:3306/mydata?serverTimezone=GMT";
//user
private static String user = "root";
//password
private static String password = "123456";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
//1)注册驱动程序
Class.forName("com.mysql.cj.jdbc.Driver");
//2)获取连接
conn = DriverManager.getConnection(url, user, password);
//3)准备sql语句
String sql = "CREATE TABLE st(id INT,NAME VARCHAR(20),age INT)";
//4)在连接基础上,创建Statement
stmt = conn.createStatement();
//5)执行sql,返回结果
int count = stmt.executeUpdate(sql);
System.out.println("影响了" + count + "行!");
//6)关闭资源.后打开的先关闭
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
网友评论