第一次写博客,希望大家支持一下,会持续更新Java学习内容,下一期预测还是jdbc,稍微对本次内容进行改进,谢谢
首先当然是加载驱动Class.forName(“com.mysql.jdbc.Driver”)
然后是建立连接的过程Connection conn = DriverManager.getConnection(url, user, password);Connection conn=DriverManage.getConnection("jdbc:mysql://localhost/student1?seUnicode=true&characterEncoding=UTF8", "root", "root")表示连接的数据库是本地的student数据库
连接上数据库之后就可以对数据库进行增删查改的操作,下面将一一举例:
查询数据库信息:
public class SelectElement {
public static void main(String[] args) {
//第一步是加载jdbc驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
System.err.println("数据库驱动加载失败"+e.getMessage());//这里出错通常是应为没有正确导入jdbc的jar包
return;//如果数据库驱动加载失败后边的操作都是无用的
}
//第二部是建立数据库的连接
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost/study?seUnicode=true&characterEncoding=UTF8", "root", "root");
} catch (SQLException e) {
System.err.println("数据库连接异常"+e.getMessage());
}
try {
stmt=conn.prepareStatement("select * from t_students");
rs=stmt.executeQuery();
while(rs.next()){
int id=rs.getInt("ID");
String name=rs.getString("Name");
String gend=rs.getString("Gendar");
String hob=rs.getString("Hobbies");
int age=rs.getInt("Age");
System.out.println("ID "+id+" Name "+name+" Gendar "+gend+" Hobbies "+hob+" Age "+age );
}
} catch (SQLException e) {
System.err.println("查询失败"+e.getMessage());
}finally{
if(rs==null){
try {
rs.close();
} catch (SQLException e) {
}
}
if(stmt==null){
try {
stmt.close();
} catch (SQLException e) {
}
}
if(conn==null){
try {
conn.close();
} catch (SQLException e) {
}
}
}
}
}
添加元素:
public class AddElement {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.err.println("数据库加载失败");
return;
}
Connection conn=null;
PreparedStatement stmt=null;
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost/study?seUnicode=true&characterEncoding=UTF8", "root", "root");
stmt=conn.prepareStatement("insert into t_students(Name,Age,Gendar)values('赵信',18,1)");
int i=stmt.executeUpdate();
System.out.println("操作成功,共有"+i+"行受到影响");
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally{
if(stmt==null){
try {
stmt.close();
} catch (SQLException e) {
}
}
if(conn==null){
try {
conn.close();
} catch (SQLException e) {
}
}
}
}
}
删除记录操作:
public class DeleteElement {
public static void main(String [] args){
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
System.out.println("驱动加载失败 ");
retutn;
}
Connection conn=null;
PreparedStatement stmt=null;
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost/study?seUnicode=true&characterEncoding=UTF8","root","root");
stmt=conn.prepareStatement("delete from t_students where name='赵信'");
int i=stmt.executeUpdate();
System.out.println("执行成功,删除了"+i+"条记录");
} catch (SQLException e) {
System.err.println("连接数据库失败");
}finally{
if(stmt==null){
try {
stmt.close();
} catch (SQLException e) {
}
}
if(conn==null){
try {
conn.close();
} catch (SQLException e) {
}
}
}
}
}
修改记录的操作:
public class UpdateElement {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
System.out.println("驱动加载失败 ");
return;
}
Connection conn=null;
PreparedStatement stmt=null;
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost/study?seUnicode=true&characterEncoding=UTF8","root","root");
stmt=conn.prepareStatement("update t_students set Age= '90'where name='赵信'");
int i=stmt.executeUpdate();
System.out.println("操作成功,共有"+i+"行受到影响");
} catch (SQLException e) {
System.err.println("数据库连接失败");
}finally{
if(stmt==null){
try {
stmt.close();
} catch (SQLException e) {
}
}
if(conn==null){
try {
conn.close();
} catch (SQLException e) {
}
}
}
}
}
浙公网安备 33010602011771号