在javaz中sqlserver连接的代码怎么修改成mysql连接代码?
packagebean;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.Res...
package bean;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcUtil {
static Connection con;
public static Connection getConnection() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433; DatabaseName=LuceneDB2";
//String username = "sa";
//String password = "1";
String username = "sa";
String password = "123456";
con = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
// 执行sql语句 增删改
public int updateExecute(String sql) {
int result = 0;
try {
Connection con = getConnection();
Statement sta = con.createStatement();
result = sta.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
// 执行sql查询语句 返回一个ResultSet
public ResultSet queryExectue(String sql) {
ResultSet rs = null;
try {
Connection con = getConnection();
Statement sta = con.createStatement();
rs = sta.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public String executeScalar(String sql) {
ResultSet rs = queryExectue(sql);
String s = "";
try {
while (rs.next()) {
s = rs.getString(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return s;
}
} 展开
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcUtil {
static Connection con;
public static Connection getConnection() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433; DatabaseName=LuceneDB2";
//String username = "sa";
//String password = "1";
String username = "sa";
String password = "123456";
con = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
// 执行sql语句 增删改
public int updateExecute(String sql) {
int result = 0;
try {
Connection con = getConnection();
Statement sta = con.createStatement();
result = sta.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
// 执行sql查询语句 返回一个ResultSet
public ResultSet queryExectue(String sql) {
ResultSet rs = null;
try {
Connection con = getConnection();
Statement sta = con.createStatement();
rs = sta.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public String executeScalar(String sql) {
ResultSet rs = queryExectue(sql);
String s = "";
try {
while (rs.next()) {
s = rs.getString(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return s;
}
} 展开
展开全部
import java.sql.*;
public class apple {
public static void main(String[] args) {
PreparedStatement ps=null;
Connection ct=null;
ResultSet rs=null;
try{
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/SMILE_OA","sa","wuh11@");
//3.设定“发送器”,功能:给数据库系统发送sql语句
ps=ct.prepareStatement("select * from 你的表");
}catch(Exception e)
{
e.printStackTrace();
}finally{
//关闭资源
try
{
if(rs!=null) rs.close();
if(ct!=null) ct.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
}
public class apple {
public static void main(String[] args) {
PreparedStatement ps=null;
Connection ct=null;
ResultSet rs=null;
try{
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/SMILE_OA","sa","wuh11@");
//3.设定“发送器”,功能:给数据库系统发送sql语句
ps=ct.prepareStatement("select * from 你的表");
}catch(Exception e)
{
e.printStackTrace();
}finally{
//关闭资源
try
{
if(rs!=null) rs.close();
if(ct!=null) ct.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |