十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
首先你要导入sqlserver的jdbc驱动jar包
成都创新互联是少有的成都做网站、网站制作、营销型企业网站、微信小程序定制开发、手机APP,开发、制作、设计、外链、推广优化一站式服务网络公司,自2013年起,坚持透明化,价格低,无套路经营理念。让网页惊喜每一位访客多年来深受用户好评
java代码如下
String JDriver ="com.microsoft.sqlserver.jdbc.SQLServerDriver"; // SQL数据库引擎
String connectDB ="jdbc:sqlserver://192.168.0.117:1433;DatabaseName=租房查询系统"; // 数据源
try {
Class.forName(JDriver);// 加载数据库引擎,返回给定字符串名的类
} catch (ClassNotFoundException e) {
// e.printStackTrace();
System.out.println("加载数据库引擎失败");
System.exit(0);
}
PreparedStatement pstm = conn.PreparedStatement("select * from t where t.name=?");
pstm.setString(1, "myName");
rs = pstm.executeQuery();
连接数据库
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class TestDB {
public static void main(String[] args) {
try {
// 1.注册驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("注册驱动成功");
String url = ""jdbc:microsoft.sqlserver://127.0.0.1:1433;DatabaseName="; //数据库IP,端口,数据库名
String username = "";
String password = "";
// 加载驱动并获取数据库连接对象
Connection connection = DriverManager.getConnection(url, username,
password);
System.out.println("连接数据库成功!");
// 创建Statement对象
Statement stmt = connection.createStatement();
// SQL语句
String sql = "insert into student(id,name,age,email,address) values(1,'tom',23,'dfg@163.com','England') ";
int rows = stmt.executeUpdate(sql);
if (rows 0) {
System.out.println("插入成功!");
}
// 释放资源
stmt.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
预编译的数据库处理
public class ConnectionManager{
public static final String DRIVER_CLASS ="";
public static final String URL = "";
public static final String USERNAME = "";
public static final String PASSWORD = "";
//获取连接对象
public static Connection getConnection(){
Connection conn = null;
Class.forName(DRIVER_CLASS);
conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
return conn;
}
public static void closeAll(ResultSet rs,Statement stmt,Connection conn){
try{
if(rs!=null){
rs.close();
rs = null;
}
if(stmt!=null){
stmt.close();
stmt = null;
}
if(conn!=null){
conn.close();
conn=null;
}
}
}
}
public class DBOperator{
public int executeUpdate(String sql,Object[] params){
int rows = 0;
Connection connn = ConnectionManager.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
if(params!=null){
for(int i=0;iparams.length;i++){
pstmt.setObject(i+1,params[i]);//下标从1开始
}
}
rows = pstmt.executeUpdate();
ConnectionManager.closeAll(null,pstmt,conn);
return rows;
}
public ResultSet executeQuery(String sql,Object[] params){
Connetion conn = ConnectionManager.getConnection();
if(params!=null){
for(int i= 0;iparams.length;i++){
pstmt.setObject(i+1,params(i));
}
}
rs=pstmt.executeQuery();
ConnectionManager.closeAll(rs,pstmt,conn);
return rs;
}
}
/**
*statement连接数据库
*/
public class TestOperatorGrade{
private static void updateGrade(int id,String name,String description){
Class.forName();//注册驱动
Connection conn = DirverManager.getConnection("","","");
Statement stmt = conn.createStament();
String sql = "UPDATE grades set name = '"+name+"',description='"+description+"'WHere id="+id;
int rows = stmt.executeUpdate(sql);
}
}
这是java 程序的代码