用java代碼運行數(shù)據(jù)庫 用java代碼運行數(shù)據(jù)庫
java連接數(shù)據(jù)庫的代碼
package mysql;
成都創(chuàng)新互聯(lián)自2013年創(chuàng)立以來,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務公司,擁有項目成都網(wǎng)站設計、網(wǎng)站制作網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元麥蓋提做網(wǎng)站,已為上家服務,為麥蓋提各地企業(yè)和個人服務,聯(lián)系電話:18982081108
import java.sql.*;
/**
* @author xys
*/
public class ConnectMysql {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
String url = "jdbc:mysql://localhost:3306/databaseName";
String user = "mysqluser";
String password = "password";
String driverClass = "com.mysql.cj.jdbc.Driver";
Connection connection = null;
Class.forName(driverClass);
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
if (connection != null) {
System.out.println("數(shù)據(jù)庫連接成功");
} else {
System.out.println("數(shù)據(jù)庫連接失敗");
connection.close();
}
return connection;
}
public void getResult() throws ClassNotFoundException, SQLException {
// 實例化 Statement 對象
Statement statement = getConnection().createStatement();
// 要執(zhí)行的 Mysql 數(shù)據(jù)庫操作語句(增、刪、改、查)
String sql = "";
// 展開結(jié)果集數(shù)據(jù)庫
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
// 通過字段檢索
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
// 輸出數(shù)據(jù)
System.out.println("ID : " +id);
System.out.println("name :" + name);
}
// 完成后需要依次關(guān)閉
resultSet.close();
statement.close();
getConnection().close();
}
}
如何通過Java代碼操作數(shù)據(jù)庫
//此類為連接數(shù)據(jù)庫并進行數(shù)據(jù)庫的操作
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Conn {
private static Connection conn = null;
private static Statement st = null;
private static ResultSet rs = null;
//建立數(shù)據(jù)庫的連接
如何在java中運行數(shù)據(jù)庫
姑且當樓主的程序用的是JDBC連接數(shù)據(jù)庫吧(不貼代碼讓人猜是真不好回答啊),樓主全文搜索
"DBManager.getConnection",就會找到下面這樣的一句話:
DBManager.getConnection("....................");
打點的部分也可能是參數(shù)寫的,反正就是找到了這么句話吧,重點就是這個打點的地方,參數(shù)的話,請看參數(shù)內(nèi)容,內(nèi)容大致如下:
jdbc:[某種數(shù)據(jù)庫]://[IP地址]:[端口號]/[庫名]?user=[賬戶]password=[密碼]
舉個mysql的例子:
jdbc:mysql://localhost:3306/test?user=rootpassword=root
這樣你就你的代碼是連接的什么數(shù)據(jù)庫了,樓主期待代碼是SQL Server數(shù)據(jù)庫吧,呵呵。
如果不是,那你就決定是自己裝一個,還是改造代碼啦?當然你還要根據(jù)別人代碼去創(chuàng)建數(shù)據(jù)庫。
給個SQL Server的例子
//加載JDBC驅(qū)動
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
//建立數(shù)據(jù)庫連接,取得Connection對象
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
String user="sa";
String password="";
Connection conn=DriverManager.getConnection(url,user,password);
//后面就可以寫操作數(shù)據(jù)庫的代碼了
如何在Java程序中訪問mysql數(shù)據(jù)庫中的數(shù)據(jù)并進行簡單的操作
一、使用工具:java語言、Myeclipse。
二、操作步驟:
1、第一步:加載MySQL的JDBC的驅(qū)動
2、第二步:創(chuàng)建與MySQL數(shù)據(jù)庫的連接類的實例
3、第三步:獲取連接類實例con,用con創(chuàng)建Statement對象類實例 sql_statement
4、第四步:執(zhí)行查詢,用ResultSet類的對象,返回查詢的結(jié)果
5、得出數(shù)據(jù)
三、注意事項:有幾處是需要根據(jù)自身情況修改的
1、如下圖中的url和賬號,密碼需要與你自己的相一致。
2、這些需要訪問的數(shù)據(jù)必須要與數(shù)據(jù)庫中的類型相互匹配,才能打印出正確的結(jié)果。
當前文章:用java代碼運行數(shù)據(jù)庫 用java代碼運行數(shù)據(jù)庫
分享鏈接:http://www.xueling.net.cn/article/docosge.html