mysql连接数据库要使用http通道,java连接数据库时需要怎么连
1个回答
展开全部
下载驱动的jar包:我用的是mysql-connector-java-5.1.8-bin.jar,并让java工程加载这个jar。
从网上摘抄的代码:
public class DBHelper {
public static final String url = "jdbc:mysql://127.0.0.1/student";
public static final String name = "com.mysql.jdbc.Driver";
public static final String user = "root";
public static final String password = "root";
public Connection conn = null;
public PreparedStatement pst = null;
public DBHelper(String sql) {
try {
Class.forName(name);//指定连接类型
conn = DriverManager.getConnection(url, user, password);//获取连接
pst = conn.prepareStatement(sql);//准备执行语句
} catch (Exception e) {
e.printStackTrace();
}
}
从网上摘抄的代码:
public class DBHelper {
public static final String url = "jdbc:mysql://127.0.0.1/student";
public static final String name = "com.mysql.jdbc.Driver";
public static final String user = "root";
public static final String password = "root";
public Connection conn = null;
public PreparedStatement pst = null;
public DBHelper(String sql) {
try {
Class.forName(name);//指定连接类型
conn = DriverManager.getConnection(url, user, password);//获取连接
pst = conn.prepareStatement(sql);//准备执行语句
} catch (Exception e) {
e.printStackTrace();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询