来来来,了解一下java的最基本的功能,连接数据库进行增删改查
怎么连接数据库呢?
1.要连接数据库受限得有支持链接数据库的jar包,放置的位置如下,此处是链接mysql的jar:
Paste_Image.png
注:如果想连接其他数据库,例如oracle可自行下载他的jar包然后放在同样的地方即可
2.代码展示:
'public static Connection getConn() throws ClassNotFoundException,SQLException {
try{
Connection con = null;
// 加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1/test";//test是数据库名称
String user = "root";//账号
String pwd = "1234qwer";//密码
// 建立连接
con = DriverManager.getConnection(url, user, pwd);
catch(Exception e){
e.printStackTrace();
}
finally{
con.close();
stm.close();
rs.close();
}
}'
那怎么判断我这个数据库到底链接上了?
简单的方法,如下:
try {
Connection con = ConnectionUtil.getConn();
System.out.println(con.isClosed());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
'
结果展示为1,证明链接失败;结果展示为0,证明链接成功
向数据库添加表格
'String sql ="create table Depart(depNo int primary key,depName varchar(50) not null,depMan varchar(50) not null) ";
Connection con;
int i = 0;
try {
con = ConnectionUtil.getConn();
Statement st = (Statement) con.createStatement();// 创建sql执行对象
int rs = st.executeUpdate(sql);//执行sql
System.out.println(rs);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}'
表格中插入数据
'String sql ="insert into Depart values(1,'ceshiyixia','woman')";
Connection con;
int i = 0;
try {
con = ConnectionUtil.getConn();
Statement st = (Statement) con.createStatement();// 创建sql执行对象
int rs = st.executeUpdate(sql);//执行sql
System.out.println(rs);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}'
表格中查询数据
'String sql ="select * from Depart";
Connection con;
int i = 0;
try {
con = ConnectionUtil.getConn();
Statement st = (Statement) con.createStatement();// 创建sql执行对象
ResultSet rs = st.executeQuery(sql);//执行sql
System.out.println(rs);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}'
表格中更改字段
'String sql ="alter table Depart rename column depName to depName1";
Connection con;
int i = 0;
try {
con = ConnectionUtil.getConn();
Statement st = (Statement) con.createStatement();// 创建sql执行对象
int rs = st.executeUpdate(sql);//执行sql
System.out.println(rs);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}'
表格中修改数据
'String sql ="update Depart set depNo=20171020";
Connection con;
int i = 0;
try {
con = ConnectionUtil.getConn();
Statement st = (Statement) con.createStatement();// 创建sql执行对象
int rs = st.executeUpdate(sql);//执行sql
System.out.println(rs);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}'
表格中删除数据
'String sql ="delete from Depart where depNo=20171020 ";
Connection con;
int i = 0;
try {
con = ConnectionUtil.getConn();
Statement st = (Statement) con.createStatement();// 创建sql执行对象
int rs = st.executeUpdate(sql);//执行sql
System.out.println(rs);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}'
删除表格
'String sql ="drop table Depart ";
Connection con;
int i = 0;
try {
con = ConnectionUtil.getConn();
Statement st = (Statement) con.createStatement();// 创建sql执行对象
int rs = st.executeUpdate(sql);//执行sql
System.out.println(rs);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}'
删除数据库
想要知道代码,请听下回分解~~~其实就是sql语句的不同,看了这么多例子发现是不是都一样呀,希望对入门的你有所帮助,谢谢观看,后续会努力增加更精彩的内容!