當前位置:首頁 » 操作系統 » jdbcmysql源碼

jdbcmysql源碼

發布時間: 2022-04-29 01:37:42

A. jdbc連接mysql的代碼

java">publicConnectioncon=null;
publicPreparedStatementpst=null;
publicConnectiongetCon()throwsSQLException{
StringJDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL驅動
try{
Class.forName(JDriver).newInstance();
Stringurl="jdbc:sqlserver://localhost:1433;DatabaseName=StudentManagement";//資料庫的信息
con=DriverManager.getConnection(url,"登錄名","密碼");
}catch(InstantiationException|IllegalAccessException
|ClassNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returncon;
}

B. java中的jdbc 怎麼實現mysql 增刪改查 jdbc連接mysql有是缺點 mysql確定不收費嗎

標准SQL都是一樣的,在網上下載一些連接MYSQL資料庫的源碼就行了

C. JAVA JDBC與MYSQL問題 看代碼

在你的.modify(a);方法里。conn = DBUtil.getConnection(); 你看看是不是返回的是null我懷疑是這里就出現問題了。

D. 如何找到mysql-jdbc驅動源碼

在工程中右鍵新建file,命名為jdbc.properties

創建完畢如圖:

在jdbc.properties文件中輸入如下信息,分別是資料庫的驅動,連接,用戶名和密碼

新建JdbcTest2.java類

輸入如下代碼:

代碼說明:
這段代碼是讀取配置文件,把配置文件中的各個項通過名稱讀取出來

這段代碼是通過反射來創建Driver對象,反射就是類的實例化

在主函數中輸入如下,測試方法

運行之後的結果如下,表示連接成功!

E. 你有一個簡單的資料庫的源代碼嗎最好用Java實現的...

class ConnectionProvider{
private static String JDBC_DRIVER;
private static String DB_URL;
private static String DB_USER;
private static String DB_PASSWORD;

public ConnectionProvider()
{
JDBC_DRIVER = "com.mysql.jdbc.Driver"
DB_URL = "jdbc:mysql://localhost:3306/u-disk";
DB_USER = "root";
DB_PASSWORD = "root"
};
public Connection getConnection()
{
try {
Class.forName(JDBC_DRIVER);
} catch (Exception e) {
System.out.println("驅動文件路徑有誤!");
}
}
Connection con = null;
try {
con = DriverManager.getConnection(DB_URL, DB_USER,
DB_PASSWORD);
} catch (SQLException e) {
System.out.println("資料庫連接建立異常!\n@shy2850@" + e.getMessage() +
e.getCause());
}
System.out.println("得到連接:Connection " + ConnectionPool.connections.size() + 1);
return new ConnectionImpl(con);
}
}

可以使用這個包裝的資料庫連接數據源在DAO工具類中使用:

package com.jdbc;

import java.sql.*;

/**課題:封裝資料庫的增刪改查的工具類的實現。
*
* 假設相關資料庫的表結構如下:
* 表名:user
* 列名及屬性:id(int 自增),name(varchar(20)),tele(char(12)),birthday(date)
* @author shy2850
*/
public class UserDAO {

Connection conn;

public UserDAO(Connection conn) {
this.conn = conn;
}

public int save(User user) throws SQLException {
String sql = "insert into user values(0,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getName());
pstmt.setString(2, user.getTele());
pstmt.setDate(3, user.getBirthday());
int n = pstmt.executeUpdate();
pstmt.close();
return n;
}

public int delete(User user) throws SQLException{
String sql = "delete from user where id = "+user.getId();
Statement stmt = conn.createStatement();
int n = stmt.executeUpdate(sql);
stmt.close();
return n;
}

public int update(User user) throws SQLException{
String sql = "update user set name=?, tele=?, birthday=? where id = "+user.getId();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(2, user.getName());
pstmt.setString(3, user.getTele());
pstmt.setDate(4, user.getBirthday());
int n = pstmt.executeUpdate(sql);
pstmt.close();
return n;
}

public User getUser(Integer id) throws SQLException{
String sql = "select * from user where id = " + id;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
User user = getUserFromResultSet(rs);
rs.close();
stmt.close();
return user;
}

static User getUserFromResultSet(ResultSet rs) throws SQLException{
Integer id = rs.getInt("id");
String name= rs.getString("name");
String tele= rs.getString("tele");
Date birthday = rs.getDate("birthday");
return new User(id, name, tele, birthday);
}
}
/**
* 構建資料庫表的java類映射
*/
class User{
private Integer id;
private String name;
private String tele;
private Date birthday;

public User() {
}
public User(Integer id, String name, String tele, Date birthday) {
super();
this.id = id;
this.name = name;
this.tele = tele;
this.birthday = birthday;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getTele() {
return tele;
}

public void setTele(String tele) {
this.tele = tele;
}

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}

F. 求基於 JSP和javabean技術 用JDBC訪問sqlserver 或mysql的 網上商城系統 急需畢業論文和源碼 !!非常感謝

package dataBase;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DBHelper {
private Connection con;
private PreparedStatement pstmt;
private ResultSet rs;
//靜態語句塊,載入驅動
static {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

public ArrayList findBySql(String sql, ArrayList<String> params, Class<?> c) {
con = this.getCon();
try {
pstmt=con.prepareStatement(sql);
doParams(pstmt, params);
rs=pstmt.executeQuery();
while(rs.next()){
/**************************************************/
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return List;
}
//連接資料庫
public Connection getCon() {
try {
con = DriverManager.getConnection(
"jdbc:sqlserver://192.168.2.30:1433;databaseName=資料庫名",
"連接資料庫名", "密碼");
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
//關閉所有連接
public void closeAll(ResultSet rs, PreparedStatement pstmt, Connection con) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//對數據進行增、刪、改
// insert into xx values(?,?); params={"張三","a"};
public void doUpdate(String sql, List<String> params) {
con = this.getCon();
try {
pstmt = con.prepareStatement(sql);
doParams(pstmt, params);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll(null, pstmt, con);
}
}
public void doParams(PreparedStatement pstmt, List<String> params)
throws SQLException {
if (pstmt != null && params != null && params.size() > 0) {
for (int i = 0; i < params.size(); i++) {
pstmt.setString(i + 1, params.get(i));
}
}
}
}

G. 分別寫出jdbc連oracle和mysql的主要代碼

JDBC連接不同資料庫的寫法如下:

1、Oracle8/8i/9i資料庫(thin模式)
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl為資料庫的SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);

2、SQL Server2005及以上版本資料庫
Class.forName("com.microsoft.sqlserver.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb為資料庫
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);

3、MySQL資料庫
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://localhost/myDB?
user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
//myDB為資料庫名
Connection conn= DriverManager.getConnection(url);

4、DB2資料庫
Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
String url="jdbc:db2://localhost:5000/sample"; //sample為你的資料庫名
String user="admin";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);

5、Sybase資料庫
Class.forName("com.sybase.jdbc.SybDriver").newInstance();
String url =" jdbc:sybase:Tds:localhost:5007/myDB";//myDB為你的資料庫名
Properties sysProps = System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn= DriverManager.getConnection(url, SysProps);

6、Informix資料庫
Class.forName("com.informix.jdbc.IfxDriver").newInstance();
String url = "jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;
user=testuser;password=testpassword"; //myDB為資料庫名
Connection conn= DriverManager.getConnection(url);
7、PostgreSQL資料庫
Class.forName("org.postgresql.Driver").newInstance();
String url ="jdbc:postgresql://localhost/myDB" //myDB為資料庫名
String user="myuser";
String password="mypassword";
Connection conn= DriverManager.getConnection(url,user,password);

8、access資料庫直連用ODBC的

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
String url="jdbc:odbc:Driver={MicroSoft Access Driver
(*.mdb)};DBQ="+application.getRealPath("/Data/ReportDemo.mdb");
Connection conn = DriverManager.getConnection(url,"","");
Statement stmtNew=conn.createStatement() ;

H. eclipse怎麼關聯mysql驅動的源碼

工具:

eclipse

方法:

  1. 在工程中右鍵新建file,命名為jdbc.properties

I. 如何使用jdbc連接mysql資料庫

用Jdbc連接MySql伺服器還是很方便的。
首先,將jdbc導入工程,或者將jdbc放到ClassPath里,這里我利用Eclipse直接導入jdbc jar文件,不羅嗦了。
然後,制定DriverManager,利用最簡單的方法,Class類的froName直接完成,代碼:
Class.forName("com.mysql.jdbc.Driver").newInstance();

然後,實例化一個鏈接Connection,注意用戶名和密碼,有幾個方法可供選擇,這里我用的是DirverManager類的getConnection(String url, String user, String password)方法。具體使用:DriverManager
例如:Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/mydatabase", "root", "1234");
下一步,建立用於執行sql語句的Statement,這個容易,一行代碼搞定:
Statement stat=conn.createStatement();
最後就可以利用stat實例執行sql語句了

熱點內容
如何把復制存儲刪了 發布:2024-10-04 21:07:59 瀏覽:202
我的解壓神器 發布:2024-10-04 21:06:06 瀏覽:110
西門子編程100例 發布:2024-10-04 21:05:28 瀏覽:326
樂高機器人ev3編程 發布:2024-10-04 20:56:10 瀏覽:990
演算法左神 發布:2024-10-04 20:23:55 瀏覽:910
lol手游如何配置技能 發布:2024-10-04 20:17:11 瀏覽:861
伺服器兩根心跳線ip一樣嗎 發布:2024-10-04 20:17:03 瀏覽:554
java無狀態 發布:2024-10-04 20:15:40 瀏覽:729
電信為什麼限制上傳速度 發布:2024-10-04 20:11:28 瀏覽:816
編程哪個培訓機構好 發布:2024-10-04 19:55:14 瀏覽:60