本文示例可見一斑了,主要是通過Java對SQL語句進行操作,和普通的增刪改查的原理是一樣的: 註:參考鏈接:http://blog.csdn.net/sd4015700/article/details/39668583 ...
本文示例可見一斑了,主要是通過Java對SQL語句進行操作,和普通的增刪改查的原理是一樣的:
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", "123456"); 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(); } }
註:參考鏈接:http://blog.csdn.net/sd4015700/article/details/39668583