Java数据库连接(JDBC)

Posted keyman-li

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java数据库连接(JDBC)相关的知识,希望对你有一定的参考价值。

 

Java数据库连接(JDBC)

JDBC:Java DataBase Connectivity
mysql SQLyog 和 Java客户端都可以和SQL进行交互

JDBC连接数据库的步骤

1.添加数据库驱动jar包支持
2.连接数据库
-username
-password
-connectionUrl

例如:

public static void testConnect() throws Exception
{
//连接MySQL服务器
String username = "root";
String password = "a1b2c3";
String connectionUrl = "jdbc:mysql://127.0.0.1:3306/af_school?useUnicode=true&characterEncoding=UTF-8";
 
Connection conn = DriverManager.getConnection(connectionUrl,username,password);
System.out.println("连接成功");
 
conn.close();
System.out.println("关闭连接");
 
}

JDBC查询数据

例如:

package my;
 
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
 
import com.mysql.jdbc.Statement;
 
public class Test
{
public static void testConnect() throws Exception
{
//连接MySQL服务器
String username = "root";
String password = "a1b2c3";
String connectionUrl = "jdbc:mysql://127.0.0.1:3306/af_school?useUnicode=true&characterEncoding=UTF-8";
 
Connection conn = DriverManager.getConnection(connectionUrl,username,password);
System.out.println("连接成功");
 
//数据库查询 Statement语句,ResultSet结果集
Statement stmt = (Statement) conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM student");
//如果有数据,则rs.next()为true
while (rs.next())
{
int id = rs.getInt("id");
String name = rs.getString("name");
String phone = rs.getString("phone");
Date birthday = rs.getDate("birthday");
 
System.out.println(id + " " + name + " " + phone + " " + birthday);
}
 
conn.close();
System.out.println("关闭连接");
 
}
 
public static void main(String[] args)
{
try
{
testConnect();
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
 
}

JDBC的插入数据

例如:

//连接MySQL服务器
String username = "root";
String password = "a1b2c3";
String connectionUrl = "jdbc:mysql://127.0.0.1:3306/af_school?useUnicode=true&characterEncoding=UTF-8";
 
Connection conn = DriverManager.getConnection(connectionUrl,username,password);
System.out.println("连接成功");
 
//插入数据
String sql = "INSERT INTO student(`id`,`name`,`birthday`)"
+ "VALUES(20181200,‘韩‘,‘1998-5-6‘)";
System.out.println("SQL:" + sql);
 
Statement stmt = (Statement) conn.createStatement();
stmt.execute(sql);
int count = stmt.getUpdateCount();
System.out.println("受影响的行数为:" + count);
 
conn.close();
System.out.println("关闭连接");
处理自增主键:

当主键设置为自增时,
1.插入数据时,不写该字段
2.执行时指定RETURN_GENERATED_KEYS
3.取出返回的自增主键

例如:

//插入一条数据
String sql = "INSERT INTO leave_event(`stuId`,`daysFrom`,`daysTo`,`type`,`reason`)"
+ "VALUES(20181010,‘2018-5-6‘,‘2018-5-20‘,‘1‘,‘出国‘)";
System.out.println("SQL:" +sql);
 
Statement stmt = (Statement) conn.createStatement();
stmt.execute(sql,Statement.RETURN_GENERATED_KEYS);
 
//取得自动生成的主键的值
ResultSet rs = stmt.getGeneratedKeys();
while (rs.next())
{
int id = rs.getInt(1);
System.out.println("产生的主键为:" + id);
}

 

JDBC的Statement接口提供的executeQuery executeUpdate execute三个方法的区别:

executeQuery:用于产生单个结果集的语句,利于SELECT语句
executeUpdate:用于执行INSERT DELETE UPDATE 以及SQL DDL(数据定义语言)语句
execute:用于执行返回多个结果集、多个更新计数或者二者组合的语句

@%28MySQL%u7B14%u8BB0%29%0A%23Java%u6570%u636E%u5E93%u8FDE%u63A5%uFF08JDBC%uFF09%0AJDBC%3AJava%20DataBase%20Connectivity%0AMySQL%20%20SQLyog%20%20%u548C%20%20Java%u5BA2%u6237%u7AEF%u90FD%u53EF%u4EE5%u548CSQL%u8FDB%u884C%u4EA4%u4E92%0A%0A%23%23%23%23JDBC%u8FDE%u63A5%u6570%u636E%u5E93%u7684%u6B65%u9AA4%0A%091.%u6DFB%u52A0%u6570%u636E%u5E93%u9A71%u52A8jar%u5305%u652F%u6301%0A%092.%u8FDE%u63A5%u6570%u636E%u5E93%0A%09%09-username%0A%09%09-password%0A%09%09-connectionUrl%0A%0A%u4F8B%u5982%uFF1A%0A%0A%09%09public%20static%20void%20testConnect%28%29%20throws%20Exception%0A%09%09%7B%0A%09%09%09//%u8FDE%u63A5MySQL%u670D%u52A1%u5668%0A%09%09%09String%20username%20%3D%20%22root%22%3B%0A%09%09%09String%20password%20%3D%20%22a1b2c3%22%3B%0A%09%09%09String%20connectionUrl%20%3D%20%22jdbc%3Amysql%3A//127.0.0.1%3A3306/af_school%3FuseUnicode%3Dtrue%26characterEncoding%3DUTF-8%22%3B%0A%09%09%0A%09%09Connection%20conn%20%3D%20DriverManager.getConnection%28connectionUrl%2Cusername%2Cpassword%29%3B%0A%09%09System.out.println%28%22%u8FDE%u63A5%u6210%u529F%22%29%3B%0A%09%09%0A%09%09conn.close%28%29%3B%0A%09%09System.out.println%28%22%u5173%u95ED%u8FDE%u63A5%22%29%3B%0A%09%09%0A%09%7D%0A%0A%23%23%23%23JDBC%u67E5%u8BE2%u6570%u636E%0A%0A%u4F8B%u5982%uFF1A%0A%0A%09package%20my%3B%0A%0A%09import%20java.sql.Connection%3B%0A%09import%20java.sql.Date%3B%0A%09import%20java.sql.DriverManager%3B%0A%09import%20java.sql.ResultSet%3B%0A%09%0A%09import%20com.mysql.jdbc.Statement%3B%0A%09%0A%09public%20class%20Test%0A%09%7B%0A%09%09public%20static%20void%20testConnect%28%29%20throws%20Exception%0A%09%09%7B%0A%09%09%09//%u8FDE%u63A5MySQL%u670D%u52A1%u5668%0A%09%09%09String%20username%20%3D%20%22root%22%3B%0A%09%09%09String%20password%20%3D%20%22a1b2c3%22%3B%0A%09%09%09String%20connectionUrl%20%3D%20%22jdbc%3Amysql%3A//127.0.0.1%3A3306/af_school%3FuseUnicode%3Dtrue%26characterEncoding%3DUTF-8%22%3B%0A%09%09%09%0A%09%09Connection%20conn%20%3D%20DriverManager.getConnection%28connectionUrl%2Cusername%2Cpassword%29%3B%0A%09%09System.out.println%28%22%u8FDE%u63A5%u6210%u529F%22%29%3B%0A%09%09%0A%09%09//%u6570%u636E%u5E93%u67E5%u8BE2%20%20Statement%u8BED%u53E5%uFF0CResultSet%u7ED3%u679C%u96C6%0A%09%09Statement%20stmt%20%3D%20%28Statement%29%20conn.createStatement%28%29%3B%0A%09%09ResultSet%20rs%20%3D%20stmt.executeQuery%28%22SELECT%20*%20FROM%20student%22%29%3B%0A%09%09//%u5982%u679C%u6709%u6570%u636E%uFF0C%u5219rs.next%28%29%u4E3Atrue%0A%09%09while%20%28rs.next%28%29%29%0A%09%09%7B%0A%09%09%09int%20id%20%3D%20rs.getInt%28%22id%22%29%3B%0A%09%09%09String%20name%20%3D%20rs.getString%28%22name%22%29%3B%0A%09%09%09String%20phone%20%3D%20rs.getString%28%22phone%22%29%3B%0A%09%09%09Date%20birthday%20%3D%20rs.getDate%28%22birthday%22%29%3B%0A%09%09%09%0A%09%09%09System.out.println%28id%20+%20%22%5Ct%22%20+%20name%20+%20%22%5Ct%22%20+%20phone%20+%20%22%5Ct%22%20+%20birthday%29%3B%0A%09%09%7D%0A%09%09%0A%09%09conn.close%28%29%3B%0A%09%09System.out.println%28%22%u5173%u95ED%u8FDE%u63A5%22%29%3B%0A%09%09%0A%09%7D%0A%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%0A%09%7B%0A%09%09try%0A%09%09%7B%0A%09%09%09testConnect%28%29%3B%0A%09%09%7D%20catch%20%28Exception%20e%29%0A%09%09%7B%0A%09%09%09//%20TODO%20Auto-generated%20catch%20block%0A%09%09%09e.printStackTrace%28%29%3B%0A%09%09%7D%0A%09%7D%0A%0A%09%7D%0A%0A%23%23%23%23JDBC%u7684%u63D2%u5165%u6570%u636E%0A%u4F8B%u5982%uFF1A%0A%0A%60%60%60%0A//%u8FDE%u63A5MySQL%u670D%u52A1%u5668%0AString%20username%20%3D%20%22root%22%3B%0AString%20password%20%3D%20%22a1b2c3%22%3B%0AString%20connectionUrl%20%3D%20%22jdbc%3Amysql%3A//127.0.0.1%3A3306/af_school%3FuseUnicode%3Dtrue%26characterEncoding%3DUTF-8%22%3B%0A%09%09%09%09%0AConnection%20conn%20%3D%20DriverManager.getConnection%28connectionUrl%2Cusername%2Cpassword%29%3B%0ASystem.out.println%28%22%u8FDE%u63A5%u6210%u529F%22%29%3B%0A%09%09%09%09%0A//%u63D2%u5165%u6570%u636E%0AString%20sql%20%3D%20%22INSERT%20INTO%20student%28%60id%60%2C%60name%60%2C%60birthday%60%29%22%0A%09%09%09%09%09%09+%20%22VALUES%2820181200%2C%27%u97E9%27%2C%271998-5-6%27%29%22%3B%0ASystem.out.println%28%22SQL%3A%22%20+%20sql%29%3B%0A%09%09%09%09%0AStatement%20stmt%20%3D%20%28Statement%29%20conn.createStatement%28%29%3B%0Astmt.execute%28sql%29%3B%0Aint%20count%20%3D%20stmt.getUpdateCount%28%29%3B%0ASystem.out.println%28%22%u53D7%u5F71%u54CD%u7684%u884C%u6570%u4E3A%uFF1A%22%20+%20count%29%3B%0A%09%09%09%09%0Aconn.close%28%29%3B%0ASystem.out.println%28%22%u5173%u95ED%u8FDE%u63A5%22%29%3B%0A%60%60%60%0A%0A%0A%0A%0A%0A%3E%23%23%23%23%23%u5904%u7406%u81EA%u589E%u4E3B%u952E%uFF1A%0A%3E%u5F53%u4E3B%u952E%u8BBE%u7F6E%u4E3A%u81EA%u589E%u65F6%uFF0C%0A%3E1.%u63D2%u5165%u6570%u636E%u65F6%uFF0C%u4E0D%u5199%u8BE5%u5B57%u6BB5%0A%3E2.%u6267%u884C%u65F6%u6307%u5B9ARETURN_GENERATED_KEYS%0A%3E3.%u53D6%u51FA%u8FD4%u56DE%u7684%u81EA%u589E%u4E3B%u952E%0A%0A%u4F8B%u5982%uFF1A%0A%60%60%60%0A//%u63D2%u5165%u4E00%u6761%u6570%u636E%0AString%20sql%20%3D%20%22INSERT%20INTO%20leave_event%28%60stuId%60%2C%60daysFrom%60%2C%60daysTo%60%2C%60type%60%2C%60reason%60%29%22%0A%09%09%09%09+%20%22VALUES%2820181010%2C%272018-5-6%27%2C%272018-5-20%27%2C%271%27%2C%27%u51FA%u56FD%27%29%22%3B%0ASystem.out.println%28%22SQL%3A%22%20+sql%29%3B%0A%09%09%0AStatement%20stmt%20%3D%20%28Statement%29%20conn.createStatement%28%29%3B%0Astmt.execute%28sql%2CStatement.RETURN_GENERATED_KEYS%29%3B%0A%09%09%0A//%u53D6%u5F97%u81EA%u52A8%u751F%u6210%u7684%u4E3B%u952E%u7684%u503C%0AResultSet%20rs%20%3D%20stmt.getGeneratedKeys%28%29%3B%0Awhile%20%28rs.next%28%29%29%0A%7B%0A%09int%20id%20%3D%20rs.getInt%281%29%3B%0A%09System.out.println%28%22%u4EA7%u751F%u7684%u4E3B%u952E%u4E3A%uFF1A%22%20+%20id%29%3B%0A%7D%0A%60%60%60%0A%0A%3Cbr%20/%3E%0A%3E%23%23%23%23%23JDBC%u7684Statement%u63A5%u53E3%u63D0%u4F9B%u7684executeQuery%20executeUpdate%20execute%u4E09%u4E2A%u65B9%u6CD5%u7684%u533A%u522B%uFF1A%0A%3EexecuteQuery%uFF1A%u7528%u4E8E%u4EA7%u751F%u5355%u4E2A%u7ED3%u679C%u96C6%u7684%u8BED%u53E5%uFF0C%u5229%u4E8ESELECT%u8BED%u53E5%0A%3EexecuteUpdate%uFF1A%u7528%u4E8E%u6267%u884CINSERT%20DELETE%20UPDATE%20%u4EE5%u53CASQL%20DDL%uFF08%u6570%u636E%u5B9A%u4E49%u8BED%u8A00%uFF09%u8BED%u53E5%0A%3Eexecute%uFF1A%u7528%u4E8E%u6267%u884C%u8FD4%u56DE%u591A%u4E2A%u7ED3%u679C%u96C6%u3001%u591A%u4E2A%u66F4%u65B0%u8BA1%u6570%u6216%u8005%u4E8C%u8005%u7EC4%u5408%u7684%u8BED%u53E5%20%20%20%20%20%20%20%0A%0A%0A%0A%0A%0A






以上是关于Java数据库连接(JDBC)的主要内容,如果未能解决你的问题,请参考以下文章

Java_JDBC连接数据库

JDBC(Java数据库连接)

求用jdbc 连接数据库的java代码(只连接部分即可)

jdbc连接数据库步骤有哪些

java 如何使用jdbc连接mysql

Java数据库连接(JDBC)