導航:首頁 > 編程大全 > javasqlserver創建資料庫

javasqlserver創建資料庫

發布時間:2025-04-13 06:52:19

java程序創建數據源怎麼做

jdbc資料庫連接:1.載入驅動Class.forName(「xxxDriver」)敗悶2建立連接:Connection conn= DriverManager.getConnection(url,user,password);(url是連接地址ip埠號和資料庫實例名,user用戶名,password密碼)3獲取statement對象:Statement stmt=conn.createStatement();4通過Statement執行Sql語句:stmt.executeQquery(String sql)會返回查詢結果集,stmt.executeUpdate(String sql)返回int型,表示影響記錄的條數;5處理結果:ResultSet rs=str.executeQuery(String sql);while(rs.next()){
System.out.println(rs.getInt(id));
}
5:關閉數據源:rs.close();
下面是連接各種資料庫的方法:
1、Oracle8/8i/9i資料庫(thin模式)
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl為資料庫的SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);

2、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);

3、Sql Server7.0/2000資料庫
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb為資料庫
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);

4、迅枯如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);

5、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);

6、MySQL資料庫
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
//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、JDBC-ODBC橋
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:jsp");
jsp為建立的odbc數據源名,事先要先將SQL server的表設置為數據源。在「管理工具」-「數據源odbc」里用系統DNS添加。

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

9.DB2資料庫
//import java.sql.*;
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);
Statement stmtNew=conn.createStatement();

10.Sql Server7.0/2000資料庫
//import java.sql.*;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
//String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db2"; //7.0、2000
String url="jdbc:sqlserver://localhost:1433;DatabaseName=db2"; //2005
//db2為資料庫名
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmtNew=conn.createStatement();

11.Sybase資料庫
//import java.sql.*;
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);
Statement stmtNew=conn.createStatement();

12.Informix資料庫
//import java.sql.*;
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);
Statement stmtNew=conn.createStatement();

13.MySQL資料庫
//import java.sql.*;
//Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Class.forName("com.mysql.jdbc.Driver");
//String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1";
String url ="jdbc:mysql://localhost:3306/myDB";

//myDB為資料庫名
Connection conn= DriverManager.getConnection(url,"root","root");
Statement stmtNew=conn.createStatement();

14.PostgreSQL資料庫
//import java.sql.*;
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);
Statement stmtNew=conn.createStatement();

15.access資料庫直連用ODBC的
//import java.sql.*;
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,"sa","");
Statement stmtNew=conn.createStatement();

16.程序計時
long time1=System.currentTimeMillis();
long time2=System.currentTimeMillis();
long interval=time2-time1;

17.延時
try {
Thread.sleep(Integer.Parse(%%1));
} catch(InterruptedException e) {
e.printStackTrace();
}

18.連接Excel文件
//import java.sql.*;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:driver={Microsoft Excel Driver (*.xls)};DBQ=D:\\myDB.xls"; // 不設置數據源
String user="myuser";
String password="mypassword";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmtNew=conn.createStatement();

❷ 怎麼使用JAVA連接資料庫

1、首先我們先建好資料庫,然後建立好程序的目錄,因為是適用於初內學者的,容所以就建立一個簡單的java project,如圖。

❸ 如何用java 連接 sqlserver 資料庫

本文將介紹使用java連接sqlserver資料庫


工具/材料

myeclipse 、 SqlServer資料庫


方法:

1、要向連接資料庫,首先應該保證資料庫服務打開

2、資料庫服務打開之後就可以在環境中編寫連接代碼了。如圖:


連接資料庫就是這兩個步驟:1)載入驅動、2)創建連接。

注意在導包是導入的java.sql下的。

接下來直接運行一下就可以測試是否連接成功了

❹ 如何使用java代碼創建資料庫實例,

java連接資料庫主要用的是jdbc。
在你指定jdbc的時候需要指定連接字元串。
對於資料庫的不同連接字元串的格式不同。
但是目前發現jdbc連接資料庫都是指定庫名稱的。比如mysql會指定。oracle會指定。sqlserver也會指定。可能是為了安全期間資料庫開發商沒有提供此類不用指定庫名就連接上的方法,主要是為了安全資料庫的安全。個人想法。

目前java連接資料庫動態建表是沒有問題的。只要是標準的sql92語法的都能實現與執行。 但是好像sql92沒有定義如何建庫的語句吧。

還有你需要先確定表和庫的概念。。 表的合集是庫。。。

❺ 用java設計一個資料庫管理系統

import java.sql.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
//import java.awt.event.*;
public class DisplayDemo extends JFrame
//implements ActionListener
{
// private String dbURL="jdbc:microsoft:sqlserver://202.115.26.181:1433"; // 資料庫標識名
private String user="devon"; // 資料庫用戶
private String password="book"; // 資料庫用戶密碼
private JTable table;
private JButton ok,canel;
public DisplayDemo(){
super("顯示資料庫查詢結果"); //調用父類構造函數
String[] columnNames={"用戶名","年齡","性別","Email"}; //列名
Object[][] rowData=new Object[5][4]; //表格數據
ok=new JButton("確定");
canel=new JButton("取消");
// ok.addActionListener(this);
// canel.addActionListener(this);
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:example","sa","");
// Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //載入驅動器
// Connection con=DriverManager.getConnection(dbURL,user,password); //獲取連接
String sqlStr="select * from users"; //查詢語句
PreparedStatement ps=con.prepareStatement(sqlStr); //獲取PreparedStatement對象
ResultSet rs=ps.executeQuery(); //執行查詢
String name,sex,email; //查詢結果
int age;
int count=0;
while (rs.next()){ //遍歷查詢結果
rowData[count][0]=rs.getString("name"); //初始化數組內容
rowData[count][1]=Integer.toString(rs.getInt("age"));
rowData[count][2]=rs.getString("sex");
rowData[count][3]=rs.getString("email");
count++;
}

con.close(); //關閉連接
}
catch(Exception ex){
ex.printStackTrace(); //輸出出錯信息
}

Container container=getContentPane(); //獲取窗口容器
//container.setLayout(null);
container.add(ok);container.add(canel); ok.setBounds(10,120,70,20);
canel.setBounds(100,120,70,20);
table=new JTable(rowData,columnNames); //實例化表格
table.getColumn("年齡").setMaxWidth(25); //設置行寬
container.add(new JScrollPane(table),BorderLayout.CENTER); //增加組件

setSize(300,200); //設置窗口尺寸
setVisible(true); //設置窗口可視
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //關閉窗口時退出程序
}

public static void main(String[] args){
new DisplayDemo();
}
}

-----------------
這是其中一個例子 你照著上面做就可以了

❻ sql server2012 jdbc如何連接資料庫

步驟分為3部:
1.通過sql server 配置管理器配置1433埠
2.將sqljdbc41.jar類庫添加到對應的工程中
3.在java程序中連接資料庫

步驟1:打開sql server 配置管理器,點擊TCP/IP右鍵,選擇啟用。將禁用的TCP/IP協議打開。
然後重啟sql server(mssqlserver)服務,使得tcp/ip協議生效。

步驟2:到microsoft官網下載sqljdbc41.jar類庫。http://www.microsoft.com/zh-CN/download/details.aspx?id=11774

將下載的壓縮包解壓,找到sqljdbc41.jar類庫即可。

然後進入eclipse界面,找到的當前工程文件,點擊右鍵,選中properties->Libraries->add external jars->找到我們剛剛下載到的sqljdbc41.jar類庫,添加即可。

步驟3:

import java.sql.*;
public class test3 {

public static void main(String[] args) {
// TODO Auto-generated method stub
PreparedStatement ps=null; //(這里也可以使用statement,視情況而定)
Connection ct=null;
ResultSet rs=null;

try {

//1.載入驅動
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1433;databaseName=test1";
String user="sa";//sa超級管理員
String password="654321";//密碼
//2.連接
ct=DriverManager.getConnection( url,user,password);
//3.創建發送端

pstmt = conn.prepareStatement("INSERT INTO staff(name, age) VALUES (?, ?)");

//通過PreparedStatement對象里的set方法去設置插入的具體數值

pstmt.setString(1, newen);

pstmt.setInt(2, 25);

pstmt.executeUpdate();

//插入成功提示

System.out.println("成功插入一條數據記錄!");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{

//關閉資源
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

jdbc連接資料庫OK!!!

閱讀全文

與javasqlserver創建資料庫相關的資料

熱點內容
手機文件系統自動退出 瀏覽:916
網路驗證防破解製作 瀏覽:755
win10創意者沒有密鑰 瀏覽:921
高仿蘋果6s能用多久 瀏覽:147
ug編程如何一條線往復銑 瀏覽:448
數控編程步距行距什麼意思 瀏覽:812
解壓出錯說沒有找到壓縮文件 瀏覽:402
考試通下載的文件在哪裡 瀏覽:838
佳能ip1880驅動程序 瀏覽:63
presariov3700升級 瀏覽:926
ps2bios日版文件下載 瀏覽:942
手機桌面文件名字怎麼不清楚了 瀏覽:289
清除exe文件夾病毒win10 瀏覽:455
彩鉛畫入門教程視頻 瀏覽:303
企業用網路直播平台做什麼 瀏覽:940
gta5win10全屏打字 瀏覽:394
30天的表格數據如何累計遞增 瀏覽:848
鏡像系統文件怎麼安裝 瀏覽:417
oppo手機卸載的app在哪裡可以找到 瀏覽:326
道路代碼大全 瀏覽:507

友情鏈接