JDBC基础(sql sever和mysql与Java的连接)
JDBC概念Java DataBase Connectivity(Java数据连接)的简称,是一种可用于执行SQL语句的java API。Java程序《-----》Java应程序编程入口andJDBC《------》数据库(增删改查)JDBC功能与数据库建立连接向数据库发送SQL语句处理数据返回的结果JDBC APIconnection (连接)sta...
JDBC概念
Java DataBase Connectivity(Java数据连接)的简称,是一种可用于执行SQL语句的java API。
Java程序《-----》Java应程序编程入口andJDBC《------》数据库(增删改查)
JDBC功能
- 与数据库建立连接
- 向数据库发送SQL语句
- 处理数据返回的结果
JDBC API
- connection (连接)
- statement (声明)
- preparedstatement (接口,编译预处理语句)
- resultset (结果集)
JDBC实现原理
- JDBC驱动管理器 java.sql.DriverManager类 负责注册特定JDBC驱动器,以及与特定驱动器建立与数据库的连接
- JDBC驱动器API java.sql.Dirver接口
- JDBC驱动器(程序) 负责与特定的数据库进行连接
JDBC驱动程序分类
- JDBC-ODBC桥式
- 本地API半java驱动程序
- 中间数据访问服务器
- 纯Java驱动程序
JDBC类与接口
- JDBC有俩个程序包 java.sql:核心包生成连接,执行SQL语句,预处理申请了语句 javax.api:扩展包,主要为数据库方面的高级操作提供接口和类
-
JDBC常用接口and类
-
Driver 接口:在内部创建连接
-
Drivermanager 接口:装入所需的驱动程序,编程是调用他的方法来创建链接
-
Connection 接口:使用该类创建ResultSet对象,PreparedStatement对象
-
Statement 接口:使用该类对象得到ResultSet(结果集)对象
-
PreparedStatement 接口:预处理SQL语句接口
-
Result 接口:结果集接口
-
ResultSetMetaData 接口:结果集的元素数据接口
-
DatabaseMetaData 接口:数据库的源数据接口
Drivermanager使用Driver创建Connection生成Statement返回ResultSet生成ResultSetMetaData
连接数据库的步骤
- 创建数据源(使用JDBC-ODBC桥式驱动程序是必须)
- 注册加载特定驱动程序
- 创建连接----Connection对象
- 利用Connection对象生成Statement对象
- 利用Statement对象执行SQL语句,增删改查
- 若执行查询语句还要从ResultSet读取数据
-
关闭ResultSet,Statement,Connection等一系列东西(对象)
这是连接成功的源码(使用sqlsever2008):
package shixun;
import java.util.List;
import java.net.ConnectException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLDataException;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
//import java.util.Scanner;
//import java.util.Set;
import javax.xml.soap.SAAJResult;
import org.omg.CORBA.PUBLIC_MEMBER;
public class Shujuku {
String drivernameString="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String urlString="jdbc:sqlserver://localhost:1433;DatabaseName=stumessag";
String usString="sa";
String password="123456";
Connection connection = null;
public Shujuku(){
try {
Class.forName(drivernameString).newInstance();
System.out.println("加载数据库驱动成功");
// connection=DriverManager.getConnection(urlString,usString,password);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
System.out.println("加载失败");
}
}
Connection ceratCinnection(){
try {
connection=DriverManager.getConnection(urlString, usString, password);
connection.setAutoCommit(true);
System.out.println("数据库连接成功");
return connection;
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
System.out.println("数据库连接失败");
return null;
}
}
public void closeConnection() {
// TODO 自动生成的方法存根
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
System.out.println("关闭连接失败");
}
}
}
public void zj(StudentMessage e){
Connection conn= null;
conn = ceratCinnection();
String sqlString="insert into ALLxinxi values('"+e.getXhsString()+"','"+e.getNameString()+"','"+e.getDhsString()+"',"
+ "'"+e.getGdsx()+"','"+e.getSjjg()+"','"+e.getJsjzcyl()+"','"+e.getJavacxsj()+"','"+e.getMdsd()+"','"+e.getTy()+"','"+e.getZcj()+"')";
try {
Statement statement = conn.createStatement();
statement.executeUpdate(sqlString);
System.out.println("添加成功");
conn.close();
} catch (SQLException a) {
// TODO 自动生成的 catch 块
a.printStackTrace();
try {
conn.close();
} catch (SQLException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
}
public void xhsc(String number){
Connection conn=null;
conn = ceratCinnection();
// String user;
// Scanner input = new Scanner(System.in);
// user = input.next();
String sqlString="delete from ALLxinxi where 学号="+number+";";
try {
Statement statement = conn.createStatement();
statement.executeUpdate(sqlString);
System.out.println("删除成功");
conn.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
try {
conn.close();
} catch (SQLException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
}
public void xg(StudentMessage a){
Connection conn=null;
conn = ceratCinnection();
String nameString=a.getNameString();
String dhString=a.getDhsString();
String xhString=a.getXhsString();
String gdsx=a.getGdsx();
String sjjg=a.getSjjg();
String jsjzcyl=a.getJsjzcyl();
String javacxsj=a.getJavacxsj();
String mdsd=a.getMdsd();
String ty=a.getTy();
String zcj1=a.getZcj();
String string="update ALLxinxi set 姓名='"+nameString+"',电话='"+dhString+"',高等数学='"+gdsx+"',数据结构='"+sjjg+"',计算机组成原理='"+jsjzcyl+"', java程序设计='"+javacxsj+"',模电数电='"+mdsd+"',体育='"+ty+"',总成绩='"+zcj1+"' where 学号="+xhString+";";
try {
Statement statement = conn.createStatement();
statement.executeUpdate(string);
conn.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
//把所有人都列出来 "select * from stuTable_1"
public StudentMessage cha(String xh){
Connection conn=null;
conn = ceratCinnection();
// Scanner inputScanner=new Scanner(System.in);
// String xuehaoString=inputScanner.next();
String sqlString = "select * from ALLxinxi where 学号='"+xh+"'";
try {
Statement statement = conn.createStatement();
// statement.executeQuery(sqlString);
ResultSet set=statement.executeQuery(sqlString);
// while(set.next()){
set.next();
StudentMessage studentMessage=new StudentMessage();
studentMessage.setXhsString(set.getString(1));
studentMessage.setNameString(set.getString(2));
studentMessage.setDhsString(set.getString(3));
studentMessage.setGdsx(set.getString(4));
studentMessage.setSjjg(set.getString(5));
studentMessage.setJsjzcyl(set.getString(6));
studentMessage.setJavacxsj(set.getString(7));
studentMessage.setMdsd(set.getString(8));
studentMessage.setTy(set.getString(9));
studentMessage.setzcj(set.getString(10));
// }
conn.close();
return studentMessage;
} catch (SQLException e) {
// TODO 自动生成的 catch 块
System.out.println("木有这个人,老铁,换个学号试试!");
return null;
}
}
public StudentMessage chaxm(String xm){
Connection conn=null;
conn = ceratCinnection();
// Scanner inputScanner=new Scanner(System.in);
// String xuehaoString=inputScanner.next();
String sqlString1 = "select * from ALLxinxi where 姓名='"+xm+"'";
System.out.println(xm);
try {
Statement statement = conn.createStatement();
// statement.executeQuery(sqlString);
ResultSet set=statement.executeQuery(sqlString1);
// while(set.next()){
set.next();
StudentMessage studentMessage=new StudentMessage();
studentMessage.setXhsString(set.getString(1));
studentMessage.setNameString(set.getString(2));
studentMessage.setDhsString(set.getString(3));
studentMessage.setGdsx(set.getString(4));
studentMessage.setSjjg(set.getString(5));
studentMessage.setJsjzcyl(set.getString(6));
studentMessage.setJavacxsj(set.getString(7));
studentMessage.setMdsd(set.getString(8));
studentMessage.setTy(set.getString(9));
studentMessage.setzcj(set.getString(10));
// }
conn.close();
return studentMessage;
} catch (SQLException e) {
// TODO 自动生成的 catch 块
System.out.println("木有这个人,老铁,换个姓名试试!");
return null;
}
}
public List<StudentMessage> chaquan(){
Connection conn=null;
conn = ceratCinnection();
// Scanner inputScanner=new Scanner(System.in);
// String xuehaoString=inputScanner.next();
String sqlString = "select * from ALLxinxi";
List<StudentMessage> list = new ArrayList<StudentMessage>();
try {
Statement statement = conn.createStatement();
// statement.executeQuery(sqlString);
ResultSet set=statement.executeQuery(sqlString);
while(set.next()){
// set.next();
StudentMessage studentMessage=new StudentMessage();
studentMessage.setXhsString(set.getString(1));
studentMessage.setNameString(set.getString(2));
studentMessage.setDhsString(set.getString(3));
studentMessage.setGdsx(set.getString(4));
studentMessage.setSjjg(set.getString(5));
studentMessage.setJsjzcyl(set.getString(6));
studentMessage.setJavacxsj(set.getString(7));
studentMessage.setMdsd(set.getString(8));
studentMessage.setTy(set.getString(9));
studentMessage.setzcj(set.getString(10));
list.add(studentMessage);
}
conn.close();
return list;
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
return null;
}
}
>My SQL
my sql和sql sever的连接一样,都需要connection statement 只是具体URL语句不同
My sql:"jdbc:mysql://localhost:3306/数据库名";
sql sever:"jdbc:sqlserver://localhost:1433;DatabaseName=数据库名"
连接数据库类(源码)
package cn.qyc.connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.junit.Test;
public class JDBCConnection {
static Connection conn = null;
static String URL = "jdbc:mysql://localhost:3306/mysql";
//加载驱动程序 建立数据库连接
// @Test
public static Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver成功");
conn = DriverManager.getConnection(URL,"root","123456");
System.out.println(conn);
} catch (Exception e) {
// TODO: handle exception
}
return conn;
}
//关闭所有链接
public static void close() throws SQLException{
conn.close();
}
}
数据库操作类(源码)
增加
package cn.qyc.service;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
import cn.qyc.connection.JDBCConnection;
public class InforService {
Connection conn = null;
Statement stmt = null;
String sqlString = "insert into userxx (username,password) values ('qyc','1227364865')";
@Test
public void insert(){
conn = JDBCConnection.getConnection();
try {
stmt = conn.createStatement();
int row = stmt.executeUpdate(sqlString);
if(row==1){
System.out.println("添加成功!");
}else {
System.out.println("添加失败!");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
删除
等待完善
修改
查看
更多推荐
所有评论(0)