{
SqlConnection SqlConn = New SqlConnection(""Data Source=server;Initial Catalog=manager;Persist Security Info=True;User ID=sa;Password=123);
string str="insert into manager(账号,姓名,姓别,年龄,) values('"+账号textboxtext+"','"+姓名textboxtext+"','"+姓别textboxtext+"','"+年龄textboxtext+"','"+textboxtext+"')";/注意中间的单双引号/
SqlCommand SqlCmd = New SqlCommand(str, SqlConn);
SqlConn Open();
SqlCmd ExecuteNonQuery();
SqlConn close();
}
正常的话这样就可以插入数据了Linux系统下可以用sqlplus命令连接oracle数据库,方法如下:
sqlplus命令语法:
1
2
3
4
5
6
#sqlplus usr/pwd@//host:port/sid
#usr:用户名
#pwd:密码
#host:数据库服务器IP
#port:端口
#sid:数据库标识符
sqlplus命令连接Oracle示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
[oracle@mzl ~]$ sqlplus risenet/1@//19216813099:1521/risenet
SQLPlus: Release 102010 - Production on Mon Feb 18 15:53:34 2008
Copyright (c) 1982, 2005, Oracle All rights reserved
Connected to:
Oracle9i Enterprise Edition Release 92010 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 92010 - Production
#测试数据库select查询语句
SQL> select instance_name,status from v$instance;1、在SQL SERVER服务器上安装MYSQL ODBC驱动;
>1)连接Oracle 8/8i/9i/10g/11g(thin模式)
ClassforName("oracleJDBCdriverOracleDriver")newInstance();
String url="JDBC:oracle:thin:@localhost:1521:orcl" //orcl为Oracle数据库的SID
String user="test";
String password="test";
Connection con=DriverManagergetConnection(url,user,password);
2)连接DB2数据库
ClassforName("comibmdb2jccDB2Driver");
String url="JDBC:db2://localhost:5000/testDb";/数据库连接串/
String user="test"; String password="test";
Connection con=DriverManagergetConnection(url,user,password);
3)连接MySQL数据库
ClassforName("commysqljdbcDriver");
String url="JDBC:mysql://localhost:8080/testDB";
String user="test"; String password="test";
Connection con=DriverManagergetConnection(url,user,password);
4)连接SQL Server数据库
ClassforName("commicrosoftJDBCsqlserverSQLServerDriver");
String url="JDBC:microsoft:sqlserver://localhost:1433;DatabaseName=testDb";
String user="test"; String password="test";
Connection con=DriverManagergetConnection(url,user,password);
5)连接PostgreSQL数据库
ClassforName("orgpostgresqlDriver");
String url="JDBC:postgresql://localhost/testDb";
String user="test"; String password="test";
Connection con=DriverManagergetConnection(url,user,password);
6)连接Access数据库
ClassforName("sunjdbcodbcJdbcOdbcDriver");
String url="JDBC:odbc:Driver={Microsoft Access Driver (mdb)};DBQ="+applicationgetRealPath("/Data/testDb/mdb");
Connection conn=DriverManagergetConnection(url,"","");
7连接Sybase数据库
ClassforName("comsybaseJDBCSybDriver");
String url="JDBC:sybase:Tds:localhost:5007/testDb";
Properties pro=SystemgetProperties();
proput("user","userId");
proput("password","user_password");
Connection con=DriverManagergetConnection(url,pro);
8连接informix数据库
ClassforName("cominformixJDBCifxDriver");
String url="JDBC:informix-sqli:localhost:1533/testDb:INFORMIXSERVER=myserver"user=testUser;password=testpassword"; Connection con=DriverManagergetConnection(url);
示例:
连接SQL Server2008R2数据库
首先Build Path → 添加外部sqljdbcjar驱动
import javasql;
public class DB {
public static void main(String[] args) throws Exception {
ClassforName("commicrosoftsqlserverjdbcSQLServerDriver");
Connection conn = DriverManagergetConnection("jdbc:sqlserver://localhost:1433; DatabaseName=数据库名", "sa", "1234");
Statement stmt = conncreateStatement();
ResultSet rs = stmtexecuteQuery("select from 表名");
while(rsnext()) {
Systemoutprintln("id为:" + rsgetString("id") + "name为:" + rsgetString("name"));
}
Systemoutprintln("数据库连接成功!");
rsclose();
stmtclose();
connclose();
Systemoutprintln("数据库成功关闭!");
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)