Java 链接mysql数据库操作
java链接mysql
首先加载mysql在java中的jdbc驱动
Class.forName("com.mysql.jdbc.Driver");
其次建立与mysql已有数据库建立连接
String url = "jdbc:mysql://localhost:3306/mydata?userUnicode=true&characterEncoding=utf-8&useSSL=false"
其中jdbc是调用jdbc驱动,mysql是指数据库类型此处为mysql数据库,localhost指链接地址,此处指本地mysql数据库,3306为mysql所使用的端口号,useUnicode=true&characterEncoding=utf-8是显示数据库数据时所使用的编码格式,useSSl=false制定ssl 链接为关闭,在较高版本中必须制定ssl是否连接,否则会报错
Strinf user = “root”; String psaawd = "123456"
user为mysql用户名,passwd为密码
Connection con = DriverManager.getConnection(url,user,passwd);
作为初始化的一部分,DriverManager 类会尝试加载在 "jdbc.drivers" 系统属性中引用的驱动程序类
getConnection(String url, String user, String password) 方法试图建立到给定数据库 URL 的连接
自此就与数据库建立了链接
public static Connection getConnection () throws SQLException,
java.lang.ClassNotFoundException
{
//加载mysql的jdbc驱动
Class.forName("com.mysql.jdbc.Driver");
//取得连接的url和用户名和密码
String url = "jdbc:mysql://localhost:3306/mydate?useUnicode=true&characterEncoding=utf-8&useSSL=false";
String user = "root";
String passwd = "123456";
//创建与数据库的实例连接
Connection con = DriverManager.getConnection(url, user,passwd);
return con;
}
验证:
建立一个sql静态语句执行对象,可以静态执行sql语句
Statement sql = con.createStatement()Statement 静态执行sql语句,一个Strmaen对象只能打开一个ResultSet对象,因此,如果Statement读取多个ResultSet对象,必须有多个Statement对象与之相对应,如果有Statement对象打开了当前ResultSet对象,则其他读取该ResultSet对象的Statement必去隐式关闭他。
创建新的表
sql.executeUpdate("create table person(id int(4) not null,name varchar(20),sex char(1),primary key(id))");
添加新成员
sql.executeUpdate("insert into person(1,"王二","M")");
查看表中的内容
String query = "select * from student";
ResultSet result = sql.executeQuery(query);
while(result.next()){
System.out.println("Id:" + resuli.getInt("id") + " name:" + result.getString("name") + " sex:"+ result.getString("sex"));
}
断开连接
sql.close();
con.close();
1 //创建静态sql语句执行对象
2 Statement sql = con.createStatement();
3
4 //向表person中插入数据
5 sql.executeUpdate("create table person (id int(4) not null auto_increment, name varchar(20) not null default 'name', math int(2) not null default 1, primary key (id) )");
6
7 sql.executeUpdate("insert into person values('liying','w')");
8 //查询操作,用ResultSet类,返回结果,显示在控制台
9 String query = "select * from preson";
10 ResultSet result = sql.executeQuery(query);
11 while (result.next())
12 {
13 int id = result.getInt("id");
14 String name = result.getString("username");
15 String mathsorce = result.getString("sex");
16 System.out.println(" |" + id + " |" + name + " |" + mathsorce);
17 }
18 //关闭数据库并断开链接
19 sql.close();
20 con.close();
View Code
//完整代码
import java.sql.*;
public class Mysql2{
public static Connection getConnection () throws SQLException,
java.lang.ClassNotFoundException
{
//加载mysql的jdbc驱动
Class.forName("com.mysql.jdbc.Driver");
//取得连接的url和用户名和密码
String url = "jdbc:mysql://localhost:3306/mydate?useUnicode=true&characterEncoding=utf-8&useSSL=false";
String user = "root";
String passwd = "123456";
//创建与数据库的实例连接
Connection con = DriverManager.getConnection(url, user,passwd);
return con;
}
public static void main(String[] args)
{
try
{
Connection con = getConnection();
Statement sql = con.createStatement();
//如果同名数据库存在就删除
//sql.executeUpdate("insert person values(, 'liying',)");
//执行一个sql语句,生成一为student的表
sql.executeUpdate("create table student (id int(20) not null auto_int not nullt, name varchar() not null default 'name', sex char(2) not null default , primary key (id) ); ");
sql.executeUpdate("insert into student values(321,'liying','w')");
sql.executeUpdate("insert into student values(123,'wangjiawu','w')");
sql.executeUpdate("insert into student values(132,'Baili','w')");
//查询操作,用ResultSet类,返回结果
String query = "select * from student";
ResultSet result = sql.executeQuery(query);
//显示查询内容
System.out.println("Student 表中的内容:");
System.out.println("---------------------------------");
System.out.println(" 序号" + " | " + "姓名" +" | "+ "性别");
System.out.println("--------------------------------");
//对查询结果进行处理
while (result.next())
{
int id = result.getInt("id");
String name = result.getString("username");
String mathsorce = result.getString("sex");
System.out.println(" |" + id + " |" + name + " |" + mathsorce);
}
sql.close();//关闭数据库
con.close();//断开连接
}catch (java.lang.ClassNotFoundException e){
System.out.println("Error");
}catch (SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}