如何使用jdbc对sql server中的表增删改查
1个回答
展开全部
举例如下:
只需要将mysql的驱动和jar包更换一下即可。
sql server的驱动:com.microsoft.sqlserver.jdbc.SQLServerDriver
url 格式:jdbc:sqlserver://localhost:1433;" + "databaseName=aa;user=Yj;password=123456;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* add by wlshi
* add time 20170614
*/
public class test {
//db_driver
private static String db_driver = "com.mysql.jdbc.Driver";
//db_url
private static String db_url = "jdbc:mysql://localhost:3308/oaop?characterEncoding=UTF-8";
//db_user
private static String user = "root";
//db_pwd
private static String db_pwd = "oaop2014";
public static void main(String[] args) throws Exception {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
//加载驱动
Class.forName(db_driver);
//获取连接
conn = DriverManager.getConnection(db_url, user, db_pwd);
String sql = "select * from person";
st = conn.createStatement();
rs = st.executeQuery(sql);
while (rs.next()) {
//姓名
String userName = rs.getString("USER_NAME");
//userId
String userId = rs.getString("USER_ID");
System.out.println(userName);
System.out.println(userId);
}
System.out.println();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs!=null){
rs.close();
}
if(st!=null){
st.close();
}
if(conn!=null){
conn.close();
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询