编写db.properties文件

  

    #MySQL配置信息

    username=root

    password=123456

    driver=com.mysql.jdbc.Driver

    url=jdbc:mysql://localhost:3306/Demo

  说明:username是你当时安装数据库时创建的名字,密码也是自己设定的。当然,你还要将mysql-connector-java-5.1.7-bin.jar这个jar包导入你的编译器,否则driver没有,连不上数据库。

   
下面附上代码并解析


    结构:JDBC连接Mysql数据库

  说明:所有的代码都放在Jdbc包下,Entity是一个实体类,我把数据库的信息刻画成一个类型,Mysql_Dao是一个接口和实现类,用于定义相应的功能,Test是测试这些方法的类,Tools包下是写的一个Dbutil类,用户连接数据库。

Entity包


 1 package Jdbc.Entity;
 2 
 3 public class Emp {
 4     
 5     //Attributes
 6     private int id;
 7     private String name;
 8     private String job;
 9     private int sal;
10     private int comm;
11     public Emp(){}
12     
13     //constructor
14     public Emp(int id,String name,String job,int sal,int comm){
15         this.id = id;
16         this.name = name;
17         this.job = job;
18         this.sal = sal;
19         this.comm = comm;
20     }
21     
22     //get and set methods
23     public int getId() {
24         return id;
25     }
26 
27     public void setId(int id) {
28         this.id = id;
29     }
30 
31     public String getName() {
32         return name;
33     }
34 
35     public void setName(String name) {
36         this.name = name;
37     }
38 
39     public String getJob() {
40         return job;
41     }
42 
43     public void setJob(String job) {
44         this.job = job;
45     }
46 
47     public int getSal() {
48         return sal;
49     }
50 
51     public void setSal(int sal) {
52         this.sal = sal;
53     }
54 
55     public int getComm() {
56         return comm;
57     }
58 
59     public void setComm(int comm) {
60         this.comm = comm;
61     }
62     
63     //toString method
64     @Override
65     public String toString() {
66         return "Emp{" +
67                 "id=" + id +
68                 ", name='" + name + '\'' +
69                 ", job='" + job + '\'' +
70                 ", sal=" + sal +
71                 ", comm=" + comm +
72                 '}';
73     }
74 }
View Code

相关文章: