先建一个工具包,用来存储在这连接代码:
命名成:DbUtils
具体代码:
package cn.com.sina.java.utils; import java.io.FileInputStream; import java.io.IOException; import java.sql.*; import java.util.Properties; /** * 工具类 * 1读取配置文件信息 * 2、加载数据库的驱动 * 3、用一个公有的方法,返回数据库的连接 * 4、用一个公有的方法完成数据库操作接口的关闭(释放资料源) */ public class DbUtils { //1定义四变变量用于装配置文件里的数据库信息 private static String driver; private static String url; private static String username; private static String password; //2通过一个static代码块完成加载 static { //创建Properties对象 Properties props = new Properties(); //加载配置文件 try { props.load(new FileInputStream("db.properties")); }catch (IOException e){ e.printStackTrace(); } //判断props是否为空 if (props!=null){ driver=props.getProperty("driver"); url= props.getProperty("url"); username= props.getProperty("username"); password= props.getProperty("password"); } //加载驱动 try { Class.forName(driver); }catch (ClassNotFoundException e){ e.printStackTrace(); } } //3定义一个公有的方法,用于返回数据库连接 public Connection getConnection(){ Connection conn=null; try { conn= DriverManager.getConnection(url,username,password); }catch (SQLException e){ e.printStackTrace(); } return conn; } //4自定义一个方法,用于关闭资源 public void closeDb(ResultSet rs, Statement stat,Connection conn){ try { if (rs!=null){ rs.close(); } if (stat!=null){ stat.close(); } if (conn!=null){ conn.close(); } }catch (SQLException e){ e.printStackTrace(); } } }
然后再写具体实现方法:
新键
DbUtilesAddDemo
再此里面写入如下代码:
package com.baidu.java.db; import cn.com.sina.java.utils.DbUtils; import java.sql.*; import java.util.Scanner; public class DbUtilesAddDemo { public static Statement stat; public static ResultSet rs; //1定义数据库的接口 static Connection conn; static PreparedStatement pstat; //自定义一个方法,用于完成数据添加\ public static void addDb(){ Scanner sc=new Scanner(System.in); System.out.print("用户名:"); String name=sc.next(); System.out.print("密码:"); String pwd=sc.next(); //创建工具类对象 DbUtils dbUtils = new DbUtils(); //对数据库操作 try { //通过工具类对象获取数据库的连接 conn= dbUtils.getConnection(); //定义一个sql语句 String sql="insert into duser (name,pwd) values (?,?)"; //实例化pstat对象 pstat=conn.prepareStatement(sql); //传递参数 pstat.setString(1,name); pstat.setString(2,pwd); //定义一个变量,用于接收执行返回的结果 int i= pstat.executeUpdate(); //处理结果 if (i==1){ System.out.println("添加成功"); } }catch (SQLException e){ e.printStackTrace(); }finally { dbUtils.closeDb(null,pstat, conn); } } //定义一个查询的方法 public static void selectDb(){ DbUtils dbUtils = new DbUtils(); try { conn=dbUtils.getConnection(); stat= conn.createStatement(); String sql="select * from duser"; rs=stat.executeQuery(sql); while (rs.next()){ System.out.println("用户名:"+rs.getString("name")+"密码:"+rs.getString("pwd")); } stat.close(); conn.close(); }catch (SQLException e){ e.printStackTrace(); } } //添加一个方法 public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.print("请输入操作数字:1=》添加,2=》查询"); int num=sc.nextInt(); switch (num){ case 1: addDb(); break; case 2: selectDb(); break; default: System.out.println("输入错误"); } } }
最终实现一次写入的增加查询操作:执行后演示如下:
注意:如果运行错误,注意看db.properties文件
db.properties代码如下:
driver = com.mysql.jdbc.Driver url = jdbc:mysql://localhost:3306/addressweb?characterEncoding=utf8&useSSL=false username=root password=1148485889
其中:=utf8&useSSL=false 是关闭SSL,简化执行,在实际开发中,要提高代码安全,需要打开