java连接SQL server数据库的代码如何改成连接mysql数据库的代码
源代码如下:packagecom.smile.util;importjava.sql.Connection;importjava.sql.DriverManager;im...
源代码如下:
package com.smile.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class SqlConnection {
private static Connection conn;
static {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection GetConnection(){
try {
conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=SMILE_OA", "sa", "wuh11@");
System.out.println("link sucess!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
} 展开
package com.smile.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class SqlConnection {
private static Connection conn;
static {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection GetConnection(){
try {
conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=SMILE_OA", "sa", "wuh11@");
System.out.println("link sucess!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
} 展开
2个回答
展开全部
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();
}
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用mysql驱动程序替换掉sqlserver的驱动程序:Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
将conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=SMILE_OA", "sa", "wuh11@");做一些修改就好了
将conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=SMILE_OA", "sa", "wuh11@");做一些修改就好了
追问
修改了但是出错了。。。怎么回事
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询