发表于: 2018-03-02 20:54:30

1 724


今天完成的事情:

完成编写DAO,并用maven自带的junit进行测试,购买服务器,并在上面安装mysql,使用navicate连接服务器上的mysql

编写DAO

package fourth.imple;

import fourth.com.*;
import fourth.Dao.studentDao;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import java.sql.ResultSet;

public class createStudent implements studentDao{
@Override
   public int addStudent(student student) {
Connection conn = null;
PreparedStatement ps = null;
int i= 0;
try {
conn = (Connection) mysqlConnect.getconnection();
String sql = "insert into student values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

ps = (PreparedStatement) conn.prepareStatement(sql);

ps.setInt(1,student.getId());
ps.setString(2,student.getCreate_at());
ps.setString(3,student.getUpdate_at());
ps.setString(4,student.getName());
ps.setString(5,student.getDailyLink());
ps.setInt(6,student.getQQ());
ps.setString(7,student.getOnlineNumber());
ps.setString(8,student.getMail());
ps.setInt(9,student.getPhone());
ps.setString(10,student.getEnrollmentTime());
ps.setString(11,student.getProfessionType());
ps.setString(12,student.getBrotherName());
ps.setString(13,student.getPromise());

i = ps.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally {
mysqlConnect.free(null,ps, conn);
System.out.println("close mysqlConnection");
}
return i;
}

@Override
   public int updateStudent(String name) {
Connection conn = null;
PreparedStatement ps = null;
int i = 0;
try {
conn = (Connection) mysqlConnect.getconnection();
String sql = "update student set promise = '一姐最胖' where name=?";
ps = (PreparedStatement) conn.prepareStatement(sql);
ps.setString(1,name);

i = ps.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally {
mysqlConnect.free(null,ps, conn);
System.out.println("close mysqlConnection");
}
return i;
}

@Override
   public int deleteStudent(String name) {
Connection conn = null;
PreparedStatement ps = null;
int i = 0;
try {
conn = (Connection) mysqlConnect.getconnection();
String sql = "delete from student where name=?";

ps = (PreparedStatement) conn.prepareStatement(sql);
ps.setString(1,name);
i = ps.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally {
mysqlConnect.free(null,ps,conn);
System.out.println("close mysqlConnection");
}
return i;
}

@Override
   public student findStudent(student student) {
Connection conn = null;
PreparedStatement ps = null;
student stu = null;
ResultSet rs = null;
try {
conn = (Connection) mysqlConnect.getconnection();
String sql = "select * from student where ID=? ";
ps = (PreparedStatement) conn.prepareStatement(sql);
ps.setInt(1,student.getId());
rs = ps.executeQuery() ;
stu = new student();
while (rs.next()){
stu.setId(rs.getInt(1));
stu.setCreate_at(rs.getString(2));
stu.setUpdate_at(rs.getString(3));
stu.setName(rs.getString(4));
stu.setDailyLink(rs.getString(5));
stu.setQQ(rs.getInt(6));
stu.setOnlineNumber(rs.getString(7));
stu.setMail(rs.getString(8));
stu.setPhone(rs.getInt(9));
stu.setEnrollmentTime(rs.getString(10));
stu.setProfessionType(rs.getString(11));
stu.setBrotherName(rs.getString(12));
stu.setPromise(rs.getString(13));

}
}catch (Exception e){
e.printStackTrace();
}finally {
mysqlConnect.free(null,ps,conn);
}
return student;
}

}

package fourth.com;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class mysqlConnect {
/**
    * 获取连接
    */
   public static Connection getconnection() {
String driverName = "com.mysql.jdbc.Driver";
       String url = "jdbc:mysql://127.0.0.1:3306/mydatabases?characterEncoding=UTF-8";
       String user = "root" ;
       String password = "root";
       Connection con = null ;
       try {

Class.forName(driverName);
           con = DriverManager.getConnection(url, user, password);
           System.out.println("success");
       } catch (Exception e) {
e.printStackTrace();
       }
return con ;
   }

/**
    * 关闭连接
    */
   public static void free(ResultSet rs, Statement sta, Connection con) {
try {
if(null != sta) {
sta.close();
               sta = null;
           }
} catch (Exception e) {
e.printStackTrace();
       }
try {
if(null != con) {
con.close();
           }
} catch (Exception e) {
e.printStackTrace();
       }
}
}
package fourth.Dao;

import fourth.com.student;

public interface studentDao {
public int addStudent(student student);
   public int updateStudent(String name);
   public int deleteStudent(String name);
   public student findStudent(student student);
}
package fourth.imple;

import fourth.com.student;
import junit.framework.TestCase;

public class createStudentTest extends TestCase{
private createStudent createStudent = new createStudent();
   public void testAddStudent() {
student student = new student();
       student.setId(12);
       student.setCreate_at("20180302");
       student.setUpdate_at("20180302");
       student.setName("韩阳");
       student.setDailyLink("www.baidu.com");
       student.setQQ(1234);
       student.setOnlineNumber("好好");
       student.setMail("好好");
       student.setPhone(12345);
       student.setEnrollmentTime("20170301");
       student.setProfessionType("前端工程师");
       student.setBrotherName("梁家健");
       student.setPromise("一姐最胖");

       for (int i = 0; i<5; i++){
createStudent.addStudent(student);
       }
}

public void testUpdateStudent() {
String name = "秦栩章";
       createStudent.updateStudent(name);
   }

public void testDeleteStudent() {
String name = "韩阳";
       createStudent.deleteStudent(name);
   }

public void testFindStudent() {
student student = new student();
       student.setId(12);
       createStudent.findStudent(student);
   }
}
package fourth.com;

public class student {
private int ID;
   private String create_at;
   private String update_at;
   private String name;
   private String dailyLink;
   private int QQ;
   private String onlineNumber;
   private String mail;
   private int phone;
   private String enrollmentTime;
   private String professionType;
   private String brotherName;
   private String promise;

   public Integer getId() {
return ID;
   }
public void setId(Integer ID) {
this.ID = ID;
   }

public String getName() {
return name;
   }
public void setName(String name) {
this.name = name;
   }

public String getCreate_at() {
return create_at;
   }
public void setCreate_at(String create_at) {
this.create_at = create_at;
   }

public String getUpdate_at() {
return update_at;
   }
public void setUpdate_at(String update_at) {
this.update_at = update_at;
   }

public String getDailyLink() {
return dailyLink;
   }
public void setDailyLink(String dailyLink) {
this.dailyLink = dailyLink;
   }

public String getOnlineNumber() {
return onlineNumber;
   }
public void setOnlineNumber(String onlineNumber) {
this.onlineNumber = onlineNumber;
   }

public String getEnrollmentTime() {
return enrollmentTime;
   }
public void setEnrollmentTime(String enrollmentTime) {
this.enrollmentTime = enrollmentTime;
   }

public String getProfessionType() {
return professionType;
   }
public void setProfessionType(String professionType) {
this.professionType = professionType;
   }

public String getMail() {
return mail;
   }
public void setMail(String mail) {
this.mail = mail;
   }

public String getBrotherName() {
return brotherName;
   }
public void setBrotherName(String brotherName) {
this.brotherName = brotherName;
   }

public String getPromise() {
return promise;
   }
public void setPromise(String promise) {
this.promise = promise;
   }

public int getQQ() {
return QQ;
   }
public void setQQ(int QQ) {
this.QQ = QQ;
   }

public int getPhone() {
return phone;
   }
public void setPhone(int phone) {
this.phone = phone;
   }
}


连接测试效果


服务器上安装mysql并用navicate连接


服务器上安装mysql

明天计划的事情:

继续精炼DAO代码,初步认识mybatis


遇到的问题:

navicate连接不上服务器的mysql,后来叫客服

现在各大云服务器都涨价了,华为云对新用户有五折优惠,建议购买

买到后需要在安全组中添加3306端口

然后依然无法访问,网上各种教程瞎说,如下才是解决之道

http://blog.csdn.net/bingjianit/article/details/54384299

http://blog.csdn.net/qq_32144341/article/details/52403388

 现在可以通过修改动态IP进行登陆,继续修改配置


收获:

修改服务器配置


返回列表 返回列表
评论

    分享到