import java.sql.*; public class Test { public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"; Connection conn = DriverManager.getConnection(url, "root", "root"); Statement stat = conn.createStatement(); //创建数据库hello stat.executeUpdate("create database hello"); //打开创建的数据库 stat.close(); conn.close(); url = "jdbc:mysql://localhost:3306/hello?useUnicode=true&characterEncoding=utf-8"; conn = DriverManager.getConnection(url, "root", "123456"); stat = conn.createStatement(); //创建表test stat.executeUpdate("create table test(id int, name varchar(80))"); //添加数据 stat.executeUpdate("insert into test values(1, '张三')"); stat.executeUpdate("insert into test values(2, '李四')"); //查询数据 ResultSet result = stat.executeQuery("select * from test"); while (result.next()) { System.out.println(result.getInt("id") + " " + result.getString("name")); } //关闭数据库 result.close(); stat.close(); conn.close(); } }
原始的jdbc连接以及操作数据库增删改查
猜你喜欢
转载自blog.csdn.net/Xiaodongge521/article/details/80777184
今日推荐
周排行