JDBC链接oracle已经mysql的测试
1、鏈接oracle
package cn.itcast.mybatis.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
public class JDBCTest {
public static void main(String[] args) {
Connection con = null;
Connection conn = null;
PreparedStatement pst = null;
Statement sql_statement = null;
try {
Long startTime = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SS");
TimeZone t = sdf.getTimeZone();
t.setRawOffset(0);
sdf.setTimeZone(t);
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@10.1.8.21:1521:QTDS";
conn = DriverManager.getConnection(url, "doudouqian", "doudouqian");
System.out.println("獲取連接"+conn);
}catch(Exception e){
e.printStackTrace();
}
}
}
2、鏈接mysql:
package cn.itcast.mybatis.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @author muyeliubing 2016-11-10
* */
public class JdbcTest {
public static void main(String[] args) {
//數據庫連接
Connection connection = null;
//預編譯的Statement,使用預編譯的Statement提高數據庫性能
PreparedStatement preparedStatement = null;
//結果 集
ResultSet resultSet = null;
try {
//加載數據庫驅動
Class.forName("com.mysql.jdbc.Driver");
//通過驅動管理類獲取數據庫鏈接
connection = DriverManager.getConnection("jdbc:mysql://10.138.60.64:3306/ddq?characterEncoding=utf-8", "JDev", "JDev");
System.out.println(connection);
//定義sql語句 ?表示占位符
String sql = "select * from user where username = ?";
//獲取預處理statement
preparedStatement = connection.prepareStatement(sql);
//設置參數,第一個參數為sql語句中參數的序號(從1開始),第二個參數為設置的參數值
preparedStatement.setString(1, "aa");
//向數據庫發出sql執行查詢,查詢出結果集
resultSet = preparedStatement.executeQuery();
//遍歷查詢結果集
while(resultSet.next()){
System.out.println(resultSet.getString("id")+" "+resultSet.getString("username")+" "
+ resultSet.getString("birthday"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//釋放資源
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(preparedStatement!=null){
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
轉載于:https://www.cnblogs.com/muliu/p/6050994.html
總結
以上是生活随笔為你收集整理的JDBC链接oracle已经mysql的测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: const 内联 枚举 宏
- 下一篇: Intellij Idea上传本地项目到