发表于: 2018-03-30 23:03:55
1 550
小白学java第十五天
前两天有点事情断断续续的学习就没写日报,今天进行总结
今天完成的事情:首先报名了武汉线下试学,师兄我去武汉咯。
这几天的基础复习发现对jdbctemplate的学习并没有直接的帮助,在请教师姐之后,我发现我jdbc基础太差, 于是继续对sql语言基础和jdbc进行了学习和消化,并自己写了代码
自己写了最基础的jdbc增操作
package com.vae.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcTest {
private final static String URL = "jdbc:mysql://localhost:3306/zyy";
public final static String USERNAME = "root";
public final static String PASSWORD = "8520963.";
public final static String DRIVER = "com.mysql.jdbc.Driver";
public static void main(String[] args) {
insert();
}
public static void insert(){
try {
//1、加载数据库驱动程序
Class.forName(DRIVER);
//2、获取数据库连接
Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
//3、构造SQL语句(插入具体的数据)
String sql = "insert into user(id,username,password,age) values(6,'熊大',55582454,52)";
//4、构造一个Statement实例(用来发送SQL语句的载体)
Statement state = connection.createStatement();
//5、执行SQL语句(其实是向数据库中发送sql语句)
state.executeUpdate(sql);
//6、关闭连接(释放资源)
state.close();
connection.close();
System.out.println("insert success");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
写了增的jdbc
package com.vae.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcQuey {
private final static String URL = "jdbc:mysql://localhost:3306/zyy";
public final static String USERNAME = "root";
public final static String PASSWORD = "8520963.";
public final static String DRIVER = "com.mysql.jdbc.Driver";
public static void main(String[] args) {
query();
}
public static void query(){
try {
Class.forName(DRIVER);
Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
String sql = "select * from sign_up_table where id=3";
Statement state = conn.createStatement();
//执行查询并返回结果集
ResultSet rs = state.executeQuery(sql);
while(rs.next()){
System.out.println("id="+rs.getObject(1)+",creat="+rs.getObject(2)+",undate="+rs.getObject(3)
+"name="+rs.getObject(4)+",QQ="+rs.getObject(5)+",职位="+rs.getObject(6)
+",\n预计入学时间="+rs.getObject(7)+",毕业学校="+rs.getObject(8)+",学院编号="+rs.getObject(9)
+",\n日志地址="+rs.getObject(10)+",宣言="+rs.getObject(11)+",线上师兄="+rs.getObject(12)
+",从哪里知道="+rs.getObject(13));
}
rs.close();
state.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
用PreparedStatement写了jdbc,封装:
package com.vee.jdbc;
public class Person {
private int ID;
private String create_at;
private String update_at;
private String name;
private String QQ;
private String iesson_type;
private String Enroiment_time;
private String Graduated_school;
private int student_ID;
private String Daily_conection;
private String wish;
private String brother_name;
private String Where_To_Undetstant;
public int getId() {
return ID;
}
public void setId(int ID) {
this.ID = ID;
}
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 getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getQQ() {
return QQ;
}
public void setQQ(String QQ) {
this.QQ = QQ;
}
public String getIesson_type() {
return iesson_type;
}
public void setIesson_type(String iesson_type) {
this.iesson_type = iesson_type;
}
public String getEnroiment_time() {
return Enroiment_time;
}
public void setEnroiment_time(String Enroiment_time) {
this.Enroiment_time = Enroiment_time;
}
public String getGraduated_school() {
return Graduated_school;
}
public void setGraduated_school(String Graduated_school) {
this.Graduated_school = Graduated_school;
}
public int getStudent_ID() {
return student_ID;
}
public void setStudent_ID(int student_ID) {
this.student_ID = student_ID;
}
public String getDaily_conection() {
return Daily_conection;
}
public void setDaily_conection(String Daily_conection) {
this.Daily_conection = Daily_conection;
}
public String getWish() {
return wish;
}
public void setWish(String wish) {
this.wish = wish;
}
public String getBrother_name() {
return brother_name;
}
public void setBrother_name(String brother_name) {
this.brother_name = brother_name;
}
public String getWhere_To_Undetstant() {
return Where_To_Undetstant;
}
public void setWhere_To_Undetstant(String Where_To_Undetstant) {
this.Where_To_Undetstant = Where_To_Undetstant;
}
public Person(int ID, String create_at, String update_at, String name, String QQ, String iesson_type,
String Enroiment_time, String Graduated_school, int student_ID, String Daily_conection,
String wish, String brother_name,String Where_To_Undetstant) {
super();
this.ID = ID;
this.create_at = create_at;
this.update_at = update_at;
this.name = name;
this.QQ = QQ;
this.iesson_type = iesson_type;
this.Enroiment_time = Enroiment_time;
this.Graduated_school = Graduated_school;
this.student_ID = student_ID;
this.Daily_conection = Daily_conection;
this.wish = wish;
this.brother_name = brother_name;
this.Where_To_Undetstant = Where_To_Undetstant;
}
public Person(String create_at, String update_at, String name, String QQ, String iesson_type,
String Enroiment_time, String Graduated_school, int student_ID, String Daily_conection,
String wish, String brother_name,String Where_To_Undetstant) {
super();
this.create_at = create_at;
this.update_at = update_at;
this.name = name;
this.QQ = QQ;
this.iesson_type = iesson_type;
this.Enroiment_time = Enroiment_time;
this.Graduated_school = Graduated_school;
this.student_ID = student_ID;
this.Daily_conection = Daily_conection;
this.wish = wish;
this.brother_name = brother_name;
this.Where_To_Undetstant = Where_To_Undetstant;
}
public Person() {
super();
}
@Override
public String toString() {
return "Person [ID=" + ID + ", create_at=" + create_at + ", update_at=" + update_at
+ ", name=" + name + ", QQ=" + QQ + ", iesson_type=" + iesson_type
+ ", Enroiment_time=" + Enroiment_time + ", Graduated_school=" + Graduated_school
+ ", student_ID=" + student_ID + ", Daily_conection=" + Daily_conection
+ ", wish=" + wish + ", brother_name=" + brother_name
+ ", Where_To_Undetstant=" + Where_To_Undetstant + "]";
}
}
写了实现
package com.vee.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class JDBCtest {
public final static String URL = "jdbc:mysql://localhost:3306/zyy?useSSL=true";
public final static String USERNAME = "root";
public final static String PASSWORD = "8520963.";
public final static String DRIVER = "com.mysql.jdbc.Driver";
public static void main(String[] args) {
// TODO Auto-generated method stub
Person p = new Person("2013-06-14 13:14:15","2013-06-14 13:14:15","刘君健","3631017","Java工程师",
"2018-04-15 13:14:15","哈尔滨工业大学",3651,
"http://www.jnshu.com/daily/54889?dailyType=others&total=7&page=1&uid=23102&sort=0&orderBy=3",
"从入门到到放弃,你确定要放弃吗?","杨以杰","知乎");
insert(p);
}
//方法:使用PreparedStatement插入数据
public static void insert(Person p){
try {
Class.forName(DRIVER);
Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
String sql = "insert into sign_up_table(create_at,update_at,name,QQ,iesson_type,Enroiment_time,"
+ "Graduated_school,student_ID,Daily_conection,wish,brother_name,Where_To_Undetstant)values(?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
//设置占位符对应的值
ps.setString(1, p.getCreate_at());
ps.setString(2, p.getUpdate_at());
ps.setString(3, p.getName());
ps.setString(4, p.getQQ());
ps.setString(5, p.getIesson_type());
ps.setString(6, p.getEnroiment_time());
ps.setString(7, p.getGraduated_school());
ps.setInt(8, p.getStudent_ID());
ps.setString(9, p.getDaily_conection());
ps.setString(10, p.getWish());
ps.setString(11, p.getBrother_name());
ps.setString(12, p.getWhere_To_Undetstant());
ps.executeUpdate();
System.out.println("MySQL操作成功");
ps.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
中间出现了很多问题,自己解决,忘了截图了
明天计划的事情:(学习jdbc封装工具类,学习编写dao实现数据库的增删查改,)
遇到的问题:(遇到很多代码错误的问题,都自己慢慢处理了,忘截图了。)
收获:(学通了jdbc,感觉对jdbctemplate的帮助会比较大,继续努力,)
评论