发表于: 2017-10-28 21:52:31

2 703


今天学习的内容
早上简单看了点书,然后开始做任务1步骤17

尽管这几天一直在补基础,但是在网上查询了下步骤17的一些资料还是看的一知半解,然后自己跟着边敲代码边理解,今天主要是创建JDBC应用程序,如下:

首先为程序导入jar包
import java.sql.*;

jar包就是别人已经写好的一些类,然后将这些类进行打包,你可以将这些jar包引入你的项目中,然后就可以直接使用这些jar包中的类和属性了,我们创建项目的时候就需要导入这些jar包,那么怎么来导入这些包?可以在网上下载,http://mvnrepository.com(maven的jar包网站)这个网站搜索到相应的jar包,然后找到相应的版本,获得jar包的dependency,把这份文件添加到pom.xml文件当中,工具就会自动下载其中的jar包,如下:

也可以直接下载jar包,放到指定路径.

然后通过Class.forName("com.mysql.jdbc.Driver");
初始化驱动类com.mysql.jdbc.Driver
public class TestJDBC {
public static void main(String[] args) {
//初始化驱动
try {
//驱动类com.mysql.jdbc.Driver
//就在 mysql-connector-java-5.1.38-bin.jar中
//如果忘记了第一个步骤的导包,就会抛出ClassNotFoundException
Class.forName("com.mysql.jdbc.Driver");
System.out.println("数据库驱动加载成功 !");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


然后建立与数据库的连接

import java.sql.*;
public class TestJDBC {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
// 建立与数据库的Connection连接
// 这里需要提供:
// 数据库所处于的ip:127.0.0.1 (本机)
// 数据库的端口号: 3306 (mysql专用端口号)
// 数据库名称 emp (我的数据库是emp)
// 编码方式 UTF-8
// 账号 root
// 密码 123456
Connection c = DriverManager
.getConnection(
"jdbc:mysql://127.0.0.1:3306/emp?characterEncoding=UTF-8",
"root", "123456");
System.out.println("连接成功,获取连接对象: " + c);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


创建Statement

Statement是执行SQL语句的,比如增删改查
import java.sql.*;
public class TestJDBC {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager
.getConnection(
"jdbc:mysql://127.0.0.1:3306/emp?characterEncoding=UTF-8",
"root", "123456");
//注意:使用的是Java.sql.Statement
//不要不小心使用到:com.mysql,jdbc.Statement;
Statement s=c.createStatement();
System.out.println("获取Statement对象: " + s);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


s.execute执行sql语句

import java.sql.*;
public class TestJDBC {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager
.getConnection(
"jdbc:mysql://127.0.0.1:3306/emp?characterEncoding=UTF-8",
"root", "123456");
Statement s = c.createStatement();
//准备sql语句
//注意:字符串要用单引号' '
String sql = "insert into bmb values(null," + "'冯亚超'" + "," + "'男'" + "," + 27 + "," + 916646626 + "," + "'漯河食品工业学院'" + "," + "'郑州'" + "," + "'知乎'" + "," + "'老大最帅'" + ")";
s.execute(sql);
System.out.println("执行插入语句成功");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


数据库的连接是有限资源,相关操作结束后,养成关闭数据库的好习惯

先关闭Statement
后关闭Connection
import java.sql.*;
public class TestJDBC {
public static void main(String[] args) {
Connection c = null;
Statement s = null;
try {
Class.forName("com.mysql.jdbc.Driver");
c = DriverManager
.getConnection(
"jdbc:mysql://127.0.0.1:3306/emp?characterEncoding=UTF-8",
"root", "123456");
s = c.createStatement();
//准备sql语句
//注意:字符串要用单引号''
String sql = "insert into bmb values(null," + "'冯亚超'" + "," + "'男'" + "," + 27 + "," + 916646626 + "," + "'漯河食品工业学院'" + "," + "'郑州'" + "," + "'知乎'" + "," + "'老大最帅'" + ")";
System.out.println(sql);
s.execute(sql);
System.out.println("执行插入语句成功");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//数据库的连接是有限资源,相关操作结束后,养成关闭数据库的好习惯
//先关闭Statement
if (s != null)
try {
s.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//后关闭Connection
if (c != null)
try {
c.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


遇到的问题

在执行插入语句时String sql = "insert into bmb values(null," + "'冯亚超'" + "," + "'男'" + "," + 27 + "," + 916646626 + "," + "'漯河食品工业学院'" + "," + "'郑州'" + "," + "'知乎'" + "," + "'老大最帅'" + ")";没看明白为什么双引号要这么用,字符串需要使用单引号,每个逗号和字符串还需要双引号,后来在师兄的帮助下明白了,如果不这么使用,那么String sql=后面的整个语句都会被当成字符串,说明我的基础还是不够,Java基本语法没有掌握好.


今天的收获

了解了pom.xml文件,学习了JDBC的创建程序过程.


明天的计划

明天继续按照task走,因为自己基础不够好,预计步骤17会卡住好几天.



返回列表 返回列表
评论

    分享到