java 数据库
一、JDBC
java database connectivity, JDBC是一套面向对象的应用程序的接口,是一个可以执行SQL语句的Java API,, 用于链接应用程序与数据库之间的桥梁。JDBC不直接访问数据库,依赖于数据库厂商提供的数据库驱动程序。
1.JDBC-ODBC
2.JDBC驱动程序的类型
二、数据库常用接口
1.connection接口
2.statement接口
3.preparestatement接口
4.DriverManager接口
5.ResultSet接口
三、数据库操作 连接、查询、模糊查询like
1.连接,顺序获取查询结果、模糊查询like
package nyClient;
import java.lang.*;
import java.sql.*;
public class mySql {
Connection connection;
//Connection con;//不能在这里定义
//Statement sqlStatement;//
static Connection con;//不能在这里定义
static Statement sqlStatement;//
static ResultSet resset;
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");//通过此函数加载jdbc驱动程序
System.out.print("数据库驱动加载成功" + "\r\n");
} catch (ClassNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
try {
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/dbs", "root","5320");
System.out.print("数据库lianjie成功"+ "\r\n");
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
return connection;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
mySql cMySql = new mySql();
con = cMySql.getConnection();
try {
sqlStatement = con.createStatement();
//正常的查询
//resset = sqlStatement.executeQuery("select * from namedata");
//模糊查询 like
resset = sqlStatement.executeQuery("select * from namedata " + " where name like '李%' ");
while(resset.next())//循环查询结果
{
String idString = resset.getString("id");
String x1 = resset.getString(2);
System.out.print("序号:" + idString );
System.out.print(" 姓名:" + name + " \n");
}
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
2.预处理
预处理主要使用通配符
3.数据库的添加、修改、删除
通过 PreparedStatement 类的指定参数动态地对数据表中原有数据进行修改操作,并通过executeUpdate()方法执行更新语句操作。
package nyClient;
import java.lang.*;
import java.sql.*;
public class mySql {
Connection connection;
//Connection con;//不能在这里定义
//Statement sqlStatement;//
static Connection con;//不能在这里定义
static Statement sqlStatement;//
static ResultSet resset;
static PreparedStatement sqlPreparedStatement;
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");//通过此函数加载jdbc驱动程序
System.out.print("数据库驱动加载成功" + "\r\n");
} catch (ClassNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
try {
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ld_ats", "root","250100");
System.out.print("数据库lianjie成功"+ "\r\n");
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
return connection;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
mySql cMySql = new mySql();
con = cMySql.getConnection();
try {
sqlPreparedStatement = con.prepareStatement("select * from qujian ");
resset = sqlPreparedStatement.executeQuery();//执行sql查询
System.out.print("开始数据的删除,修改");
while(resset.next())//循环查询结果
{
String idString = resset.getString("id");
String x1 = resset.getString(2);
String name = resset.getString("name");
System.out.print("序号:" + idString );
System.out.print(" xl:" + x1 + " name: " + name + "\n");
}
//注意,inset必须把所有的列都要写上的
sqlPreparedStatement = con.prepareStatement("insert into qujian values (?, ?, ?, ?, ?)");
sqlPreparedStatement.setInt(1, 45);//注意,第一个参数是数据库中表的列,第二个是数值
sqlPreparedStatement.setString(2, "");
sqlPreparedStatement.setString(3, "济南-青岛");
sqlPreparedStatement.setString(4, "");
sqlPreparedStatement.setString(5, "");
//executeUpdate是prepareStatement中执行sql的语句,用于添加,删除,更新
sqlPreparedStatement.executeUpdate();//更新一下
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}