• java mysql链接


    package cn.test.merlini;

    import java.sql.*;

    public class DbSample {

    /**
    * @param args
    */
    public Connection dbConnection =null;
    private String driverName;
    private String host;
    private String port;
    private String dbName;
    private String userName;
    private String password;
    private String enCoding;

    public DbSample(String host,String port,String dbName,String userName,String password)
    {
    this.driverName="com.mysql.jdbc.Driver";
    this.host=host;
    this.port=port;
    this.dbName=dbName;
    this.userName=userName;
    this.password=password;
    this.enCoding="useUnicode=true&characterEncoding=utf8";
    }

    public String getURL()
    {
    StringBuilder url=new StringBuilder();

    url.append("jdbc:mysql://");
    url.append(host);
    url.append(":");
    url.append(port);
    url.append("/");
    url.append(dbName);
    url.append("?");
    url.append("user="+userName);
    url.append("&");
    url.append("password="+password);
    url.append("&");
    url.append(enCoding);
    return url.toString();
    }

    public boolean connectionDB(String url)
    {
    try
    {
    Class.forName(this.driverName);
    // System.out.println(url);
    dbConnection=DriverManager.getConnection(url);
    System.out.println("Mysql connect success");
    return true;
    }catch(Exception e){
    System.out.println("Mysql connect000 failed");
    e.getStackTrace();
    return false;
    }


    }

    public boolean closeDB()
    {
    try
    {
    if(dbConnection !=null)
    {
    dbConnection.close();
    System.out.println("close ok");
    }
    return true;
    }catch(Exception e)
    {
    System.out.println("Mysql close failed");
    e.getStackTrace();
    return false;
    }
    }

    public boolean doSelect(String sql)
    {
    Statement statement =null;
    ResultSet result =null;
    try
    {
    statement = dbConnection.createStatement();
    result = statement.executeQuery(sql);
    while(result.next())
    {
    System.out.print(result.getInt(1)+" ");
    System.out.println(result.getString(2));
    }
    return true;
    }catch(Exception e)
    {
    System.out.println("查询失败");
    e.getStackTrace();
    return false;
    }finally
    {
    try
    {
    if(result !=null)
    result.close();
    if(statement !=null)
    statement.close();
    }catch(SQLException e)
    {
    e.printStackTrace();
    }
    }

    }

    public boolean doUpdate(String sql)
    {
    Statement statement =null;

    try
    {
    statement = dbConnection.createStatement();
    statement.executeUpdate(sql);

    return true;
    }catch(Exception e)
    {
    System.out.println("insert操作失败");
    e.getStackTrace();
    return false;
    }finally
    {
    try
    {
    if(statement !=null)
    statement.close();
    }catch(SQLException e)
    {
    e.printStackTrace();
    }
    }
    }

    public static void main(String[] args)
    {
    String sql="";
    DbSample obj=new DbSample("localhost","3306","test","admin","123456");
    obj.connectionDB(obj.getURL());
    sql="DROP TABLE IF EXISTS `student`;";
    obj.doUpdate(sql);
    sql="CREATE TABLE `student` (`NO` int(11) , `name` VARCHAR(20) DEFAULT NULL) ENGINE=INNODB DEFAULT CHARSET=utf8;";
    obj.doUpdate(sql);
    obj.doUpdate("insert into student(NO,name) values(1,'孤鸿影')");
    obj.doUpdate("insert into student(No,name) values(2,'孤鸿影1')");
    obj.doSelect("select * from student");

    try
    {

    PreparedStatement newStudent = null;

    newStudent=obj.dbConnection.prepareStatement("insert into student values(?,?)");
    newStudent.setInt(1, 1);
    newStudent.setString(2,"爱我中华");
    newStudent.executeUpdate();
    newStudent.clearParameters();

    newStudent.setInt(1, 1);
    newStudent.setString(2,"爱我中华");
    newStudent.executeUpdate();
    newStudent.clearParameters();

    newStudent.close();
    }catch(SQLException e)
    {
    e.printStackTrace();
    }


    System.out.println("使用prepareStatement更新数据后");
    obj.doSelect("select * from student");


    obj.closeDB();
    }

    }

    困难是老天给我们提高的机会,坚定不移勇敢地去攻克,不要退缩,加油!
  • 相关阅读:
    分片副本监控优化备份分词器03
    es score限制
    German Collegiate Programming Contest 2017 G. Water Testing
    SQL注入以及sqlmap初体验
    BUUCTF [极客大挑战 2019]Upload 1
    DVWA—File Upload 文件上传
    DVWA—XSS 跨站脚本攻击
    WUSTCTF2020 佛说:只能四天
    猪圈密码
    2016全国大学生信息安全竞赛 破译
  • 原文地址:https://www.cnblogs.com/merlini/p/3254306.html
Copyright © 2020-2023  润新知