新聞中心
用java編寫一個連接數(shù)據(jù)庫的程序。急需?。?!謝謝
String url = "com.mysql.jdbc.Driver";
創(chuàng)新互聯(lián)公司成立以來不斷整合自身及行業(yè)資源、不斷突破觀念以使企業(yè)策略得到完善和成熟,建立了一套“以技術(shù)為基點,以客戶需求中心、市場為導(dǎo)向”的快速反應(yīng)體系。對公司的主營項目,如中高端企業(yè)網(wǎng)站企劃 / 設(shè)計、行業(yè) / 企業(yè)門戶設(shè)計推廣、行業(yè)門戶平臺運營、成都app軟件開發(fā)、成都手機網(wǎng)站制作、微信網(wǎng)站制作、軟件開發(fā)、成都移動機房等實行標準化操作,讓客戶可以直觀的預(yù)知到從創(chuàng)新互聯(lián)公司可以獲得的服務(wù)效果。
String driver = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "12345";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(driver); // 1.注冊驅(qū)動
conn = DriverManager.getConnection(url, user, password); // 獲取數(shù)據(jù)庫連接
stmt = conn.createStatement(); // 獲取數(shù)據(jù)庫操作對象
rs = stmt.executeQuery("要執(zhí)行的SQL語句"); // 執(zhí)行SQL返回結(jié)果集
while (rs.next()) { // 逐條記錄進行訪問
Object obj = rs.getObject(1); // 獲取一條記錄的第一列
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 資源釋放
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
怎樣用JAVA語言建自己想要的數(shù)據(jù)庫
只要把數(shù)據(jù)庫的連接寫好了,自己寫增刪改查就好了。+
jdbc數(shù)據(jù)庫連接
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Database
{
public Connection conn = null;
private String url =
"jdbc:microsoft:sqlserver://localhost:1433;databaseName=test";
private String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String user = "sa";
private String pwd = "";
boolean flag;
// 加載驅(qū)動
public Database() throws Exception
{
try
{
Class.forName(driver);
}
catch (Exception ex)
{
ex.printStackTrace();
throw ex;
}
}
// 連接數(shù)據(jù)庫
public void getConn() throws Exception
{
try
{
if (conn == null || conn.isClosed())
{
this.conn = DriverManager.getConnection(url, user, pwd);
}
}
catch (Exception ex)
{
ex.printStackTrace();
throw ex;
}
}
// 關(guān)閉conn
public void closeConn(Connection conn)
{
if (conn != null)
{
try
{
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
//關(guān)閉ps
public void closePs(PreparedStatement ps)
{
if (ps != null)
{
try
{
ps.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
// 關(guān)閉rs
public void closeRs(ResultSet rs)
{
if (rs != null)
{
try
{
rs.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
// 查詢
public void getStu() throws Exception
{
PreparedStatement ps = null;
ResultSet rs = null;
try
{
this.getConn();
ps = this.conn.prepareStatement("select * from student");
rs = ps.executeQuery();
while (rs.next())
{
System.out.println(rs.getLong(1));
System.out.println(rs.getString(2));
System.out.println(rs.getLong(3));
System.out.println(rs.getString(4));
System.out.println(rs.getString(5));
}
}
catch (Exception ex)
{
ex.printStackTrace();
throw ex;
}
finally
{
if (rs != null)
{
this.closeRs(rs);
}
if (ps != null)
{
this.closePs(ps);
}
if (conn != null)
{
this.closeConn(conn);
}
}
}
}
java如何去創(chuàng)建數(shù)據(jù)庫
Java要連接數(shù)據(jù)庫,那么首先你必須安裝mysql數(shù)據(jù)庫。
安裝好mysql之后,安裝JDK了。
安裝好JDK之后,就是安裝Eclipse了,要支持JDK版本,Eclipse安裝的時候會自動去找JDK安裝位置的,解壓版的Eclipse,就要配置eclipse.ini文件了,將對應(yīng)的JDK配置好,這些已經(jīng)準備就緒的時候,就到mysql中創(chuàng)建數(shù)據(jù)庫和表。
先創(chuàng)建數(shù)據(jù)庫:
CREATE DATABASE SCUTCS;
接著,創(chuàng)建表:
CREATE TABLE STUDENT
(
SNO CHAR(7) NOT NULL,
SNAME VARCHAR(8) NOT NULL,
SEX CHAR(2) NOT NULL,
BDATE DATE NOT NULL,
HEIGHT DEC(5,2) DEFAULT 000.00,
PRIMARY KEY(SNO)
);
然后插入數(shù)據(jù),可以用SQL語句insert into 表名 values (value1, value2, ...);
編寫.java文件來演示一下如何訪問MySQL數(shù)據(jù)庫。
import java.sql.*;
public class JDBCTest {
public static void main(String[] args){
// 驅(qū)動程序名 String driver = "com.mysql.jdbc.Driver";
// URL指向要訪問的數(shù)據(jù)庫名scutcs String url = "jdbc:mysql://127.0.0.1:3306/scutcs";
// MySQL配置時的用戶名 String user = "root"; // MySQL配置時的密碼 String password = "root";
try { // 加載驅(qū)動程序 Class.forName(driver);
// 連續(xù)數(shù)據(jù)庫 Connection conn = DriverManager.getConnection(url, user, password);
if(!conn.isClosed()) System.out.println("Succeeded connecting to the Database!");
// statement用來執(zhí)行SQL語句 Statement statement = conn.createStatement();
// 要執(zhí)行的SQL語句 String sql = "select * from student";
// 結(jié)果集 ResultSet rs = statement.executeQuery(sql);
while(rs.next()) // 選擇sname這列數(shù)據(jù) name = rs.getString("sname
// 輸出結(jié)果 System.out.println(rs.getString("sno") + "\t" + name); }
rs.close(); conn.close();
} catch(ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!"); e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
} } }
本文標題:Java編寫數(shù)據(jù)庫代碼 java的數(shù)據(jù)庫
網(wǎng)站URL:http://fisionsoft.com.cn/article/ddjocec.html