Java JDBC Connection to ODBC database MySQL database

Source: Internet
Author: User
Tags odbc connection

1.JDBC Connection database to add a Mysql-connector-java-5.1.38-bin.jar library

2.ODBC connecting the database to add an ODBC data source

3. Test code

Dbhelper.java Code JDBC Connection Database

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";//user name
Public final String name = "Com.mysql.jdbc.Driver";
Public final String pswd = null;//password
Public Connection conn = null;//connection
Public PreparedStatement PST = null;//ready to execute SQL statement
Public dbhelper (String sql) {
try {

Class.forName (name);
conn = (Connection) drivermanager.getconnection (URL, username, pswd);
PST = (preparedstatement) conn.preparestatement (SQL);
System.out.println ("JDBC Link database");
} catch (Exception e) {

SYSTEM.OUT.PRINTLN ("Load driver failed" + e.tostring ());
}

}

Public PreparedStatement DBQ () {

return PST;
}
public void Closedb () {
try {
Conn.close ();
Pst.close ();
} catch (Exception e) {

SYSTEM.OUT.PRINTLN ("exception occurred");
}

}

}

Dbhelper1.java code, ODBC connection database

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 Link database! ");
} catch (Exception e) {

SYSTEM.OUT.PRINTLN ("Load driver failed" + e.tostring ());
}

}

Public PreparedStatement DBQ () {

return PST;
}

public void Closedb () {
try {

Conn1.close ();
Pst.close ();
} catch (Exception e) {

SYSTEM.OUT.PRINTLN ("exception occurred");
}

}

}

Entrance of the program: Mainui.java implementation of the program to delete and change
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 {
//************************************************************************************************//
Query information
//************************************************************************************************//
DBHelper dbhelper=new dbhelper (SQL);//Generate database objects prepare to execute SQL statements
PST=DBHELPER.DBQ ();//Return object PST
Pst.setstring (1, "1234");//Assign a value to a placeholder
ResultSet ret=pst.executequery ();//Execute SQL statement get result set
while (Ret.next ())//Check out result set
{
Id=ret.getstring (1);
Name=ret.getstring (2);
Age=ret.getstring (3);
System.out.println (id+ "," +name+ "," +age ");
}
Dbhelper.closedb ();





//************************************************************************************************//
Inserting information
//************************************************************************************************//
DBHelper dbhelper1=new DBHelper (SQL1);
PST=DBHELPER1.DBQ ();
Pst.setstring (1, "1239");
Pst.setstring (2, "Wang er");
Pst.setstring (3, "54");
int f=pst.executeupdate ();
System.out.println (f);
if (f!=0)
{
System.out.println ("Insert record successfully! ");

}
else {

System.out.println ("Insert record Failed");
}
Dbhelper1.closedb ();




//************************************************************************************************//
Modify Information
//************************************************************************************************//

DBHelper dbhelper2=new DBHelper (SQL2);
PST=DBHELPER2.DBQ ();
Pst.setstring (1, "1234");
int t=pst.executeupdate ();
if (t!=0)
{
SYSTEM.OUT.PRINTLN ("Update succeeded! ");
}
else {
System.out.println ("Update failed! ");
}
Dbhelper2.closedb ();

//**********************************************************************************************//
Delete Information
//********************************************************************************************//
DBHelper1 dbhelper3=new DBHelper1 (SQL3);
PST=DBHELPER3.DBQ ();
Pst.setstring (1, "1239");
int d=pst.executeupdate ();
if (d!=0)
{
System.out.println ("Delete succeeded! ");

}
else {
System.out.println ("Delete failed! ");
}
Dbhelper3.closedb ();

} catch (Exception e) {
System.out.println (E.tostring ());
}

}

}

Java JDBC Connection to ODBC database MySQL database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.