JAVA连接mysql数据库常用功能框架。

import java.sql.*;

/**
 * Created by meng on 2015/7/11.
 */

class myConnection{
    private String dbDriver="com.mysql.jdbc.Driver";
    private String dbUrl="jdbc:mysql://127.0.0.1:3306/test";//根据实际情况变化
    private String dbUser="root";
    private String dbPass="root";
    public java.sql.Connection getConn()
    {
        java.sql.Connection conn=null;
        try
        {
            Class.forName(dbDriver);
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        try
        {
            conn = DriverManager.getConnection(dbUrl, dbUser, dbPass);//注意是三个参数
            System.out.print("mysqlConnection.");
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return conn;
    }

    public int insert()
    {
        int i=0;
        String sql="insert into (表名)(列名1,列明2) values(?,?)";
        Connection cnn=getConn();

        try{
            PreparedStatement preStmt =cnn.prepareStement(sql);
            preStmt.setString(1,值);
            preStmt.setString(2,值);//或者:preStmt.setInt(1,值);
            i=preStmt.executeUpdate();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return i;//返回影响的行数,1为执行成功
    }
    public int update
    {
        int i=0;
        String sql="update (表名) set  (列名1)=?,列明2=? where (列名)=?";//注意要有where条件
        Connection cnn=getConn();


        try{
            PreparedStatement preStmt =cnn.prepareStatement(sql);
            preStmt.setString(1,(值));
            preStmt.setString(2,(值));//或者:preStmt.setInt(1,值);
            preStmt.setInt(3,(值));
            i=preStmt.executeUpdate();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return i;//返回影响的行数,1为执行成功
    }

    public String select
    {
        String sql = "select * from (表名) where (列名)=(值)";
        Connection cnn = getConn();//此处为通过自己写的方法getConn()获得连接
        try
        {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);

            if(rs.next())
            {
                int m1 = rs.getInt(1);//或者为rs.getString(1),根据数据库中列的值类型确定,参数为第一列
                String m2 = rs.getString(2);
            }
            //可以将查找到的值写入类,然后返回相应的对象
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return (相应的值的变量);
    }

    public int delete()
    {
        String sql = "delete from (表名) where (列名)=(值)";
        int i=0;
        Connection conn = getConn();//此处为通过自己写的方法getConn()获得连接
        try
        {
            Statement stmt = conn.createStatement();
            i = stmt.executeUpdate(sql);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return i;//如果返回的是1,则执行成功;
    }
}
public class main {

    public static void main(String[] args){
        myConnection con1 = new myConnection();
        con1.getConn();
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值