jdbc 举例

package com.you.domain; 
 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.ArrayList; 
import java.util.List; 
 
public class Domain { 
    private static String url = "jdbc:mysql://localhost:3306/test?charset=utf8"; 
    private static String username = "root"; 
    private static String password = "mysql"; 
    static { 
        try { 
            Class.forName("com.mysql.jdbc.Driver"); 
        } catch (ClassNotFoundException e) { 
            throw new ExceptionInInitializerError(e); 
        } 
    } 
     
    public static Connection getConnection() throws SQLException { 
        return DriverManager.getConnection(url,username,password); 
    } 
     
    //向数据库中插入数据 
    public void insert(String name, String word, String sex, String birth, String email, String address) { 
        Connection con = null; 
        PreparedStatement pt = null; 
        try { 
            con = getConnection(); 
            String sql = "insert into register(name,word,sex,birth,email,address) value(?,?,?,?,?,?)"; 
            pt = con.prepareStatement(sql); 
            pt.setString(1, name); 
            pt.setString(2, word); 
            pt.setString(3, sex); 
            pt.setString(4, birth); 
            pt.setString(5, email); 
            pt.setString(6, address); 
            pt.executeUpdate(); 
        } catch (SQLException e) { 
            throw new RuntimeException(); 
        }finally{ 
            if(pt != null) { 
                try { 
                    pt.close(); 
                } catch (SQLException e) { 
                    throw new RuntimeException(); 
                } 
            } 
             
            if(con != null) { 
                try { 
                    con.close(); 
                } catch (SQLException e) { 
                    throw new RuntimeException(); 
                } 
            } 
        } 
    } 
     
    //查询数据 

    public List<String> queryDB(String name) { 
        Connection cn = null; 
        PreparedStatement ps = null; 
        ResultSet rs = null; 
        List<String> list = new ArrayList<String>(); 
        try { 
            cn = getConnection(); 
            String sql = "select word from register where name = ?"; 
            ps = cn.prepareStatement(sql); 
            ps.setString(1, name); 
            rs = ps.executeQuery(); 
            while(rs.next()) { 
                list.add(rs.getString("word")); 
            } 
        } catch (SQLException e) { 
            throw new RuntimeException(); 
        }finally { 
            if(rs != null) { 
                try { 
                    rs.close(); 
                } catch (SQLException e) { 
                    throw new RuntimeException(); 
                } 
            } 
             
            if(ps != null) { 
                try { 
                    ps.close(); 
                } catch (SQLException e) { 
                    throw new RuntimeException(); 
                } 
            } 
             
            if(cn != null) { 
                try { 
                    cn.close(); 
                } catch (SQLException e) { 
                    throw new RuntimeException(); 
                } 
            } 
        } 
        return list; 
    } 
     
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值