JAVA_JDBC连接数据库

Posted Tears_fg

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA_JDBC连接数据库相关的知识,希望对你有一定的参考价值。

测试类:
1
import java.util.ArrayList; 2 import java.util.List; 3 4 /** 5 * 创建数据库: 6 * 1.加载驱动 7 * Class.forName("org.sqlite.JDBC"); 8 * 2.通过驱动管理类获得Connection对象 jdbc:sqlite:+数据库的路径 9 * Connection connection = DriverManager.getConnection("jdbc:sqlite:d:/myTest.db"); 10 * 3.通过Connection对象获得Statement对象 11 * Statement statement = connection.createStatement(); 12 * 4.通过statement对象执行sql语句,获得结果集 13 * ResultSet rSet = statement.executeQuery("select * from Student"); 14 * 15 * @author Administrator 16 * 17 */ 18 /** 19 * exc: 20 * 通过Java代码实现,创建表,并对其进行增删改查的功能 21 * 22 * 注意:需自导入 sqlitejdbc-0.5.4.jar 23 * @author Administrator 24 * !!! 25 * 如果出现以下错误:Unsupported major.minor version 52.0 26 * 解决方法: 修改编译方式,降低javaee ,javase的版本,同时让编译方式和Jdk版本对应。 27 * 右击项目 properties或者alt + enter 选择java Complier 选择 Complier compliance level的版本和当前jdk版本对应, 28 * 如果是web项目还要降低 jstl 和javaee的版本。 29 */ 30 public class Test { 31 public static void main(String[] args) { 32 TestJDBC tj = new TestJDBC(); 33 tj.createTable(); 34 tj.addUser("张三", "男"); 35 tj.addUser("李四", "女"); 36 tj.addUser("王五", "男"); 37 tj.addUser("赵六", "男"); 38 System.out.println( "增:"+tj.addUser("周七", "女")); 39 System.out.println("删:"+tj.deleteUser(4)); 40 System.out.println("改:"+tj.updateUser(2, "haha", "不详")); 41 List<User> list = tj.getAllUser(); 42 for (User user : list) { 43 System.out.println("查:"+user); 44 } 45 46 } 47 }

User类:

 1 public class User {
 2     private String name;
 3     private int _id;
 4     private String sex;
 5     public String getName() {
 6         return name;
 7     }
 8     public void setName(String name) {
 9         this.name = name;
10     }
11     public int getId() {
12         return _id;
13     }
14     public void setId(int id) {
15         this._id = id;
16     }
17     public String getSex() {
18         return sex;
19     }
20     public void setSex(String sex) {
21         this.sex = sex;
22     }
23     @Override
24     public String toString() {
25         return "User [name=" + name + ", _id=" + _id + ", sex=" + sex + "]";
26     }
27     
28     
29 }

TestJDBC类

  1 import java.sql.Connection;
  2 import java.sql.DriverManager;
  3 import java.sql.ResultSet;
  4 import java.sql.SQLException;
  5 import java.sql.Statement;
  6 import java.util.ArrayList;
  7 import java.util.List;
  8 
  9 
 10 public class TestJDBC {
 11     Connection con;//数据库一个连接对象
 12     Statement statement;//执行sql语句
 13     ResultSet rs;//得到select语句执行的结果集
 14     
 15     public Statement getStatement(){
 16         try {
 17             //加载驱动
 18             Class.forName("org.sqlite.JDBC");
 19             //获得connection对象
 20             con = DriverManager.getConnection("jdbc:sqlite:d:/mysqlite.db");//没有会自动创建一个数据库
 21             //得到Statement对象
 22             statement = con.createStatement();
 23         } catch (ClassNotFoundException e) {
 24             // TODO Auto-generated catch block
 25             e.printStackTrace();
 26         } catch (SQLException e) {
 27             // TODO Auto-generated catch block
 28             e.printStackTrace();
 29         }
 30         return statement;
 31         
 32     }
 33     public void closeConnection(){
 34         if(con!= null){
 35             try {
 36                 con.close();
 37             } catch (SQLException e) {
 38                 // TODO Auto-generated catch block
 39                 e.printStackTrace();
 40             }
 41         }
 42         if(statement != null){
 43             try {
 44                 statement.close();
 45             } catch (SQLException e) {
 46                 // TODO Auto-generated catch block
 47                 e.printStackTrace();
 48             }
 49         }
 50         
 51     }
 52     public void createTable(){
 53         statement = getStatement();
 54         try {
 55             statement.execute("create table if not exists User(_id Integer primary key autoincrement,name varchar(20),sex varchar(20))");
 56         } catch (SQLException e) {
 57             // TODO Auto-generated catch block
 58             e.printStackTrace();
 59         }finally{
 60             closeConnection();
 61         }
 62     }
 63     public boolean addUser(String name,String sex){
 64         statement = getStatement();
 65         try {
 66             //insert into User(name,age) values(‘张三‘,18)
 67             int count = statement.executeUpdate("insert into User(name,sex) values(‘"+name+"‘,‘"+sex+"‘)");
 68             if(count>0){
 69                 return true;
 70             }
 71         } catch (SQLException e) {
 72             // TODO Auto-generated catch block
 73             e.printStackTrace();
 74         }finally{
 75             closeConnection();
 76         }
 77         return false;
 78     }
 79     public boolean deleteUser(int id){
 80         statement = getStatement();
 81         try {
 82             int count = statement.executeUpdate("delete from User where _id = "+id);
 83             if(count>0)
 84                 return true;
 85         } catch (SQLException e) {
 86             // TODO Auto-generated catch block
 87             e.printStackTrace();
 88         }
 89         return false;
 90     }
 91     public boolean updateUser(int id,String name,String sex){
 92         statement = getStatement();
 93         try {
 94             int count = statement.executeUpdate("update User set name=‘"+name+"‘,sex=‘"+sex+"‘where _id ="+id);
 95             if(count>0)
 96                 return true;
 97         } catch (SQLException e) {
 98             // TODO Auto-generated catch block
 99             e.printStackTrace();
100         }
101         return false;
102     }
103     public List<User> getAllUser(){
104         List<User> list = new ArrayList<User>();
105         
106         statement =  getStatement();
107         try {
108             rs = statement.executeQuery("select * from User");
109             while(rs.next()){
110                 User user = new User();
111                 user.setName(rs.getString("name"));
112                 user.setSex(rs.getString("sex"));
113                 user.setId(rs.getInt("_id"));
114                 list.add(user);
115             }
116         } catch (SQLException e) {
117             // TODO Auto-generated catch block
118             e.printStackTrace();
119         }finally{
120             closeConnection();
121         }
122         
123         return list;
124         
125     }
126 }

 

以上是关于JAVA_JDBC连接数据库的主要内容,如果未能解决你的问题,请参考以下文章

Java_JDBC连接数据库

Java_JDBC

30_数据库_第30天java_jdbc_(DBUtils)_讲义

JAVA_JDBC1

Java_JDBC使用Statement和PreparedStatement

JDBC连接数据库的几种方法