📄 sqlserverconn.java
字号:
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;
import java.sql.*;
import java.text.*;
import java.lang.*;
import com.inet.tds.TdsDriver;
/**
* Use other default JDBC driver connection to MS SQL Server DBMSS
* the JDBC driver is Sprinta2000,you can download if from:
*http://www.inetsoftware.de/English/produkte/JDBC_Overview/ms.htm
*/
public class sqlserverconn{
/** String identifier of this connection's type */
public static void main(String[] args){
String sql_query="select * from t_employee";
String sql_insert="insert into t_employee values(1003,'zhanghz03',28,7000.00,'inset through jdbc')";
String sql_update="update t_employee set emp_name='zhanghz02' where emp_no='1002'";
String sql_delete="delete from t_employee where emp_name='zhanghz02'";
ResultSet rs;
System.out.println("start conn db");
try{
//load the class with the driver:Microsoft JDBC Driver
//Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
//open a connection to the database
//Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://hostname:port;User=sa;Password=zhangjl12345");
//you can replace the above setence by this:
//Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://hostname:port;","sa","zhangjl12345");
//use an other jdbc driver to connect SQL Server 2000
Class.forName("com.inet.tds.TdsDriver");
//you can replace the above setence by this:
//Class.forName("com.inet.tds.TdsDriver").newInstance();
//set a timeout for login and query
DriverManager.setLoginTimeout(10);
//Connection con=DriverManager.getConnection("jdbc:inetdae7a:172.24.216.55/mysqlsvrdb","sa","zhangjl");
//you can replace the above setence by this:
Connection con=DriverManager.getConnection("jdbc:inetdae7a:172.24.216.55:1079","sa","zhangjl");
//to get the driver version
DatabaseMetaData conMD = con.getMetaData();
System.out.println("Driver Name:\t" + conMD.getDriverName());
System.out.println("Driver Version:\t" + conMD.getDriverVersion());
System.out.println("conn succ\n");
Statement stmt=con.createStatement();
String st=sql_query.trim().substring(0,sql_query.indexOf(" "));
//stmt.executeUpdate(sql_insert);
//because the function of executeUpdate() is same as execute()
//so you can replace the above setence by this:
stmt.executeUpdate(sql_insert);
con.commit();
System.out.println("insert into t_employee succ");
stmt.execute(sql_update);
con.commit();
System.out.println("update t_employee succ");
stmt.executeUpdate(sql_delete);
con.commit();
System.out.println("delete t_employee succ");
rs=stmt.executeQuery(sql_query);
System.out.println("select from t_employee:");
while(rs.next()){
for(int j=1; j<=rs.getMetaData().getColumnCount(); j++){
System.out.print(rs.getObject(j)+"\t");
}
System.out.println();
}
con.close();
System.out.println("Bye-Bye");
}catch(Exception e){
System.out.println("conn failed");
}
}//end main()
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -