java访问mysql数据库

  1 package com.mysql.test;
  2 
  3 import java.sql.Connection;
  4 import java.sql.DriverManager;
  5 import java.sql.ResultSet;
  6 import java.sql.SQLException;
  7 import java.sql.Statement;
  8 
  9 public class JDBC_Test1 {
 10     // 创建静态全局变量
 11     static Connection conn;
 12 
 13     static Statement st;
 14 
 15     public static void main(String[] args) {
 16         //insert();    //插入添加记录
 17         //update();    //更新记录数据
 18         //delete();    //删除记录
 19         query();    //查询记录并显示
 20     }
 21     
 22     /* 插入数据记录,并输出插入的数据记录数*/
 23     public static void insert() {
 24         
 25         conn = getConnection();    // 首先要获取连接,即连接到数据库
 26 
 27         try {
 28             st = (Statement) conn.createStatement();    // 创建用于执行静态sql语句的Statement对象
 29             
 30             String sql = "INSERT INTO employee(num, d_id, name,age, sex, homeaddr)"
 31                     + " VALUES (8, 1007, '***', 66,'男','北京朝阳区')";    // 插入数据的sql语句
 32             
 33             int count = st.executeUpdate(sql);    // 执行插入操作的sql语句,并返回插入数据的个数
 34             
 35             System.out.println("向employee表中插入 " + count + " 条数据");    //输出插入操作的处理结果
 36             
 37             conn.close();    //关闭数据库连接
 38             
 39         } catch (SQLException e) {
 40             System.out.println("插入数据失败" + e.getMessage());
 41         }
 42     }
 43     
 44     /* 更新符合要求的记录,并返回更新的记录数目*/
 45     public static void update() {
 46         conn = getConnection();    //同样先要获取连接,即连接到数据库
 47         try {
 48             st = (Statement) conn.createStatement();    //创建用于执行静态sql语句的Statement对象,st属局部变量
 49             
 50             String sql = "update employee set name='奥巴马' where d_id = 1006";// 更新数据的sql语句
 51             
 52             int count = st.executeUpdate(sql);// 执行更新操作的sql语句,返回更新数据的个数
 53             
 54             System.out.println("employee表中更新 " + count + " 条数据");        //输出更新操作的处理结果
 55             
 56             conn.close();    //关闭数据库连接
 57             
 58         } catch (SQLException e) {
 59             System.out.println("更新数据失败");
 60         }
 61     }
 62 
 63     /* 查询数据库,输出符合要求的记录的情况*/
 64     public static void query() {
 65         
 66         conn = getConnection();    //同样先要获取连接,即连接到数据库
 67         try {
 68             st = (Statement) conn.createStatement();    //创建用于执行静态sql语句的Statement对象,st属局部变量
 69             String sql = "select * from employee";        // 查询数据的sql语句
 70             
 71             ResultSet rs = st.executeQuery(sql);    //执行sql查询语句,返回查询数据的结果集
 72             System.out.println("最后的查询结果为:");
 73             while (rs.next()) {    // 判断是否还有下一个数据
 74                 
 75                 // 根据字段名获取相应的值
 76                 int num = rs.getInt("num");
 77                 int d_id = rs.getInt("d_id");
 78                 String name = rs.getString("name");
 79                 int age = rs.getInt("age");
 80                 String sex = rs.getString("sex");
 81                 String homeaddr = rs.getString("homeaddr");
 82                 
 83                 //输出查到的记录的各个字段的值
 84                 System.out.println(num + " " + d_id + " " + name + " " + age
 85                         + " " + sex + " " + homeaddr);
 86             
 87             }
 88             conn.close();    //关闭数据库连接
 89             
 90         } catch (SQLException e) {
 91             System.out.println("查询数据失败");
 92         }
 93     }
 94 
 95     /* 删除符合要求的记录,输出情况*/
 96     public static void delete() {
 97 
 98         conn = getConnection();    //同样先要获取连接,即连接到数据库
 99         try {
100             st = (Statement) conn.createStatement();    //创建用于执行静态sql语句的Statement对象,st属局部变量
101             
102             String sql = "delete from employee  where d_id = 1005";// 删除数据的sql语句
103             
104             int count = st.executeUpdate(sql);// 执行sql删除语句,返回删除数据的数量
105             
106             System.out.println("employee表中删除 " + count + " 条数据\n");    //输出删除操作的处理结果
107             
108             conn.close();    //关闭数据库连接
109             
110         } catch (SQLException e) {
111             System.out.println("删除数据失败");
112         }
113         
114     }
115     
116     /* 获取数据库连接的函数*/
117     public static Connection getConnection() {
118         Connection con = null;    //创建用于连接数据库的Connection对象
119         try {
120             Class.forName("com.mysql.jdbc.Driver");// 加载Mysql数据驱动
121             
122             con = DriverManager.getConnection(
123                     "jdbc:mysql://localhost:3306/company", "root", "hjf19920121");// 创建数据连接
124             
125         } catch (Exception e) {
126             System.out.println("数据库连接失败" + e.getMessage());
127         }
128         return con;    //返回所建立的数据库连接
129     }
130 }
 1 package com.mysql.test;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 import java.sql.Statement;
 8 
 9 public class JDBC_test {
10     static Connection connection = null;
11     public static void main(String[] args) {
12         executeTest();
13         
14     }
15     /* 获取数据库连接的函数*/
16     public static Connection getConnection() {
17         String url = "jdbc:mysql://localhost:3306/company";//获取协议,IP和端口等信息
18         String user = "root";//获取数据库用户名
19         String password = "hjf19920121";//获取数据库用户密码
20         
21         try {
22             Class.forName("com.mysql.jdbc.Driver");//指定JDBC驱动的类型
23             connection = DriverManager.getConnection(url, user, password);
24         } catch (Exception e) {
25                 e.printStackTrace();System.out.println("数据库连接失败" + e.getMessage());
26         }
27         
28         return connection;
29     }
30     
31     public static void executeTest() {
32         connection = getConnection();//同样先要获取连接,即连接到数据库
33         try {
34             Statement statement = connection.createStatement();//创建Statement对象
35             
36             String sql = "delete from employee  where d_id = 1005";
37             
38             boolean st = statement.execute(sql);//如果sql语句是select语句,该方法返回true,如果是update,insert,delete语句该方法返回false
39             if(st==true){
40                 ResultSet result = statement.executeQuery(sql);
41                 
42                 System.out.println("最后的查询结果为:");
43                 
44                 while(result.next()){
45                     // 根据字段名获取相应的值
46                     int num = result.getInt("num");
47                     int d_id = result.getInt("d_id");
48                     String name = result.getString("name");
49                     int age = result.getInt("age");
50                     String sex = result.getString("sex");
51                     String homeaddr = result.getString("homeaddr");
52                     
53                     //输出查到的记录的各个字段的值
54                     System.out.println(num + " " + d_id + " " + name + " " + age
55                             + " " + sex + " " + homeaddr);
56                 }
57             }
58             else{
59                 int i = statement.getUpdateCount();//获取发生变化的记录数
60                 System.out.println(i);
61             }
62             connection.close();
63         } catch (SQLException e) {
64             e.printStackTrace();
65         }
66     }
67 }

 

posted @ 2014-02-24 18:41  胡椒粉hjf  阅读(364)  评论(0编辑  收藏  举报