JavaWeb JDBC连接MySql数据库小项目

环境:eclipse,mysql

1. 在mysql里面建表test

JavaWeb JDBC连接MySql数据库小项目_第1张图片

2. 导入链接mysql的jar包

JavaWeb JDBC连接MySql数据库小项目_第2张图片

3. 创建DataAccess的工具类,在这个类里面执行数据库链接操作,返回Connection这个对象。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 *  test 数据库连接操作
 * @author Administrator
 *
/
  public class DataAccess {
  private static final String driver="com.mysql.jdbc.Driver";
  private static final String url="jdbc:mysql://localhost:3306/test
                       ?useUnicode=true&characterEncoding=UTF-8&useSSL=true";
  private static final String username="root";
  private static final String passwork="2315806986";
  private   static Connection   conn=null; 

 static{
     try{
         //加载驱动
         Class.forName(driver);
     }catch(Exception ex){
         ex.printStackTrace();
     }
 }

 public static Connection getConnection()   throws Exception{
     if(conn==null){
         //连接数据库
         conn=DriverManager.getConnection(url, username, passwork);
         return conn;
     }

     //判断数据库连接是否成功
     if(conn!=null){
         System.out.println("连接正常");
     }else{
         System.out.println("连接异常");
     }

     return conn;
 }

 public static Boolean closeConnection(){
     if(conn != null){
         try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
     }
    return true;
 }

}

4. User的实体类,test数据库里面的test表有关字段组成这个类,便于对数据进行操作

public class User {
private int id;
private String name;
private int age;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}

}

5. 查询的操作类,里面包括一个quaryAll()的函数,查询数据库test表的所有数据,返回ArrayList < User > ;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class UserDAO {

/**
        * 查询数据库里面所有的数据
        * @return
     */
        public static ArrayList quaryAll(){
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    ArrayList list = new ArrayList();

    try {
        conn = DataAccess.getConnection();
        String sql = "select * from test;";
        stmt = conn.prepareStatement(sql);
        rs = stmt.executeQuery();
        while (rs.next()) {
            User item = new User();
            item.setAge(rs.getInt("age"));
            item.setName(rs.getString("name"));
            item.setId(rs.getInt("id"));
            list.add(item);
        }

        return list;
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    } finally {
        if (rs != null) {
            try {
                rs.close();
                rs = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (stmt != null) {
            try {
                stmt.close();
                stmt = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
}

6. 测试的test类,主函数,打印出所查数据。

import java.util.ArrayList;

public class Test {

public static void main(String[] args) {
    ArrayList userList = UserDAO.quaryAll();
    for(User user : userList){
        System.out.println("查询出来的id是:"+user.getId());
        System.out.println("查询出来的名字是:"+user.getName());
        System.out.println("查询出来的年龄是:"+user.getAge());
    }
}

}

7.运行结果

JavaWeb JDBC连接MySql数据库小项目_第3张图片

  • http://download.csdn.net/download/weiguohuanghun/9999640

你可能感兴趣的:(JavaWeb)