以上的代碼都不如哥的 且看哥是怎么寫條理清晰的代碼的?。?!
創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),西固企業(yè)網(wǎng)站建設(shè),西固品牌網(wǎng)站建設(shè),網(wǎng)站定制,西固網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,西固網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
package dbconnection //java 中不存在沒有包的類(講解詳細(xì)因?yàn)轫?xiàng)目需要)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;//引入sql數(shù)據(jù)庫包
public class DBConnection{
private Connection conn=null;
private Statement stmt=null;
private Result rs=null;
private String jdbc="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private String driverManager="jdbc:sqlserver://localhost:1433;databasename=HcitPos";
private String user="admin";
private int password="admin";
public DBConnection{
try{
Class.forName("jdbc");
conn.getConnection("driverManager");
}
catch(Exception e){}
}
public selectMethod(String sql){
stmt=conn.createStatement();
rs=stmt.extcuteQuery("sql");
while(rs.next()){
String title=rs.getString("title");//利用javaBean獲得數(shù)據(jù)庫中的屬性
String name=rs.getString("name");
.......
System.out.println("title");
System.out.println("name");
......
//當(dāng)然數(shù)據(jù)庫的操作有很多 這里簡單介紹下功能的實(shí)現(xiàn)
}
}
public void closeDB(){
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}
}
package mysql;
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 {
// 實(shí)例化 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();
}
}
你要連的數(shù)據(jù)庫是SQL 還是ORACLE
但是代碼都查不多
下面的是連接SQL數(shù)據(jù)庫的代碼
你需要先創(chuàng)建個(gè)數(shù)據(jù)庫,還有表,表的字段是登陸名和密碼
下面的"SA" 是登陸名 "111111" 是密碼
ORACLE 和這個(gè)查不多
import java.sql.*;//做數(shù)據(jù)庫時(shí)候必須要引入的包
import com.microsoft.jdbc.sqlserver.SQLServerDriver;
public class DBFactory {
Connection Conn=null;
PreparedStatement Stmt=null;
ResultSet Rs=null;
String driverName="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String OracleUserName="sa";
String OracleUserPwd="111111";
String ConnUrl="jdbc:sqlserver://localhost:1433;databaseName=news";
public Connection getConnection()
{
try {
Class.forName(driverName);
} catch (ClassNotFoundException ex) {
System.out.println("加載驅(qū)動(dòng)程序有錯(cuò)誤");
}
try {
Conn = DriverManager.getConnection(ConnUrl, OracleUserName,OracleUserPwd);
} catch (SQLException ex1) {
System.out.print("取得連接的時(shí)候有錯(cuò)誤,請核對用戶名和密碼");
}
return Conn;
}
這個(gè)是連接ORACLE數(shù)據(jù)庫代碼
import java.sql.*;
import oracle.jdbc.driver.OracleDriver;
public class DBFactory {
Connection Conn=null;
PreparedStatement Stmt=null;
ResultSet Rs=null;
String driverName="oracle.jdbc.driver.OracleDriver";
String OracleUserName="scott";
String OracleUserPwd="tiger";
String ConnUr1="jdbc:oracle:thin:@locahost:1521:Ora";
public Connection getConnection()
{
try {
Class.forName(driverName);
} catch (ClassNotFoundException ex) {
System.out.println("加載驅(qū)動(dòng)程序有錯(cuò)誤");
}
try {
Conn = DriverManager.getConnection(ConnUr1, OracleUserName,OracleUserPwd);
} catch (SQLException ex1) {
System.out.print("取得連接時(shí)有錯(cuò)誤,請核對用戶名和密碼");
}
return Conn;
}
希望能追加分?jǐn)?shù)謝謝!