学习完servlet终于可以学习JDBC了,下载安装完mysql,导入jar包后,就可以利用java对mysql进行一系列的操作了
连接数据库并插入数据的代码
package com.k1998;import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.cj.xdevapi.Statement;public class Game {public static void main(String[] args) throws SQLException {try {Class.forName("com.mysql.cj.jdbc.Driver"); Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/qq?serverTimezone=UTC&characterEncoding=utf-8 ","root","123456");System.out.println(conn);String sql = "insert into user (username,pwd) values ('lis',1317652)";java.sql.Statement stmt = conn.createStatement(); stmt.execute(sql);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
}