java jdbc与odbc数据库的连接mysql数据库
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java jdbc与odbc数据库的连接mysql数据库相关的知识,希望对你有一定的参考价值。
1.jdbc连接数据库要添加mysql-connector-java-5.1.38-bin.jar库
2.odbc连接数据库要添加odbc数据源
3.测试代码
DBHelper.java代码jdbc连接数据库
import java.beans.Statement;
import java.sql.DriverManager;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.sun.jndi.url.corbaname.corbanameURLContextFactory;
public class DBHelper {
public final String url = "jdbc:mysql://127.0.0.1:3306/School?useUnicode=true&characterEncoding=GBK";
public final String username = "root";//用户名
public final String name = "com.mysql.jdbc.Driver";
public final String pswd = null;//密码
public Connection conn = null;//连接
public PreparedStatement pst = null;//准备执行的sql语句
public DBHelper(String sql) {
try {
Class.forName(name);
conn = (Connection) DriverManager.getConnection(url, username, pswd);
pst = (PreparedStatement) conn.prepareStatement(sql);
System.out.println("jdbc链接数据库");
} catch (Exception e) {
System.out.println("加载驱动失败" + e.toString());
}
}
public PreparedStatement DBQ() {
return pst;
}
public void CloseDB() {
try {
conn.close();
pst.close();
} catch (Exception e) {
System.out.println("出现异常");
}
}
}
DBHelper1.java代码,ODBC连接数据库
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class DBHelper1 {
public final String url="jdbc:odbc:TEST";
public final String name = "root";
public final String psd = null;
public final String name1 = "sun.jdbc.odbc.JdbcOdbcDriver";
public Connection conn1 = null;
public PreparedStatement pst = null;
public DBHelper1(String sql) {
try {
Class.forName(name1);
conn1 = (Connection) DriverManager.getConnection(url,
name, psd);
pst = (PreparedStatement) conn1.prepareStatement(sql);
System.out.println("ODBC链接数据库!");
} catch (Exception e) {
System.out.println("加载驱动失败" + e.toString());
}
}
public PreparedStatement DBQ() {
return pst;
}
public void CloseDB() {
try {
conn1.close();
pst.close();
} catch (Exception e) {
System.out.println("出现异常");
}
}
}
程序的入口:MainUI.java实现程序的增删改查
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Set;
public class MainUI {
public static void main(String[] args) {
String name=null;
String age=null;
String id=null;
String sql="select *from teacher";
String sql1="insert into teacher values(?,?,?)";
String sql2="update teacher set age=age+1 where tid=?";
String sql3="delete from teacher where tid=?";
PreparedStatement pst=null;
try {
//************************************************************************************************//
//查询信息
//************************************************************************************************//
DBHelper dbHelper=new DBHelper(sql);//生成数据库对象准备执行sql语句
pst=dbHelper.DBQ();//返回对象pst
pst.setString(1, "1234");//为占位符赋值
ResultSet ret=pst.executeQuery();//执行sql语句获取结果集
while(ret.next())//取出结果集
{
id=ret.getString(1);
name=ret.getString(2);
age=ret.getString(3);
System.out.println(id+","+name+","+age);
}
dbHelper.CloseDB();
//************************************************************************************************//
//插入信息
//************************************************************************************************//
DBHelper dbHelper1=new DBHelper(sql1);
pst=dbHelper1.DBQ();
pst.setString(1, "1239");
pst.setString(2, "王二");
pst.setString(3, "54");
int f=pst.executeUpdate();
System.out.println(f);
if(f!=0)
{
System.out.println("插入记录成功!");
}
else {
System.out.println("插入记录失败");
}
dbHelper1.CloseDB();
//************************************************************************************************//
//修改信息
//************************************************************************************************//
DBHelper dbHelper2=new DBHelper(sql2);
pst=dbHelper2.DBQ();
pst.setString(1, "1234");
int t=pst.executeUpdate();
if(t!=0)
{
System.out.println("更新成功!");
}
else {
System.out.println("更新失败!");
}
dbHelper2.CloseDB();
//**********************************************************************************************//
//删除信息
//********************************************************************************************//
DBHelper1 dbHelper3=new DBHelper1(sql3);
pst=dbHelper3.DBQ();
pst.setString(1, "1239");
int d=pst.executeUpdate();
if(d!=0)
{
System.out.println("删除成功!");
}
else {
System.out.println("删除失败!");
}
dbHelper3.CloseDB();
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
以上是关于java jdbc与odbc数据库的连接mysql数据库的主要内容,如果未能解决你的问题,请参考以下文章
mysql驱动mysql-connector-java 属于JDBC第几类驱动?