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:用于执行返回多个结果集、多个更新计数或者二者组合的语句
以上是关于Java数据库连接(JDBC)的主要内容,如果未能解决你的问题,请参考以下文章