本文示例可见一斑了,主要是通过Java对SQL语句进行操作,和普通的增删改查的原理是一样的:
java" id="highlighter_507881">
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
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://www.cnblogs.com/kings-9/archive/2017/10/05/7629232.html