码迷,mamicode.com
首页 > 数据库 > 详细

java jdbc与odbc数据库的连接mysql数据库

时间:2016-09-03 19:50:02      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:

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数据库

标签:

原文地址:http://www.cnblogs.com/zhaiyansheng/p/5837794.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!