java+mysql搭建服务器
答案:1 悬赏:30 手机版
解决时间 2021-11-22 16:46
- 提问者网友:眉目添风霜
- 2021-11-22 01:44
java+mysql搭建服务器
最佳答案
- 五星知识达人网友:白昼之月
- 2021-11-22 02:45
java+mysql搭建服务器的步骤如下:
1、在工程项目中导入mysql-connection-java-5.0.16-bin.jar这个jar包;
2、编写一个数据库管理类(MyDBHelper)用来配置数据库的属性:
package com.hu.demo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class MyDBHelper {
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 void close() {
try {
this.conn.close();
this.pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} 3、编写一个测试类(Test)来测试相关数据库操作:
package com.hu.demo;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Test{
static String sql = null;
static DBHelper db1 = null;
static ResultSet ret = null;
public static void main(String[] args) {
sql = "select *from stuinfo";//SQL语句
db1 = new DBHelper(sql);//创建DBHelper对象
try {
ret = db1.pst.executeQuery();//执行语句,得到结果集
while (ret.next()) {
String uid = ret.getString(1);
String ufname = ret.getString(2);
String ulname = ret.getString(3);
String udate = ret.getString(4);
System.out.println(uid + " " + ufname + " " + ulname + " " + udate );
}//显示数据
ret.close();
db1.close();//关闭连接
} catch (SQLException e) {
e.printStackTrace();
}
}
} 4、至此就完全了java+mysql服务器的搭建。
1、在工程项目中导入mysql-connection-java-5.0.16-bin.jar这个jar包;
2、编写一个数据库管理类(MyDBHelper)用来配置数据库的属性:
package com.hu.demo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class MyDBHelper {
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 void close() {
try {
this.conn.close();
this.pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} 3、编写一个测试类(Test)来测试相关数据库操作:
package com.hu.demo;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Test{
static String sql = null;
static DBHelper db1 = null;
static ResultSet ret = null;
public static void main(String[] args) {
sql = "select *from stuinfo";//SQL语句
db1 = new DBHelper(sql);//创建DBHelper对象
try {
ret = db1.pst.executeQuery();//执行语句,得到结果集
while (ret.next()) {
String uid = ret.getString(1);
String ufname = ret.getString(2);
String ulname = ret.getString(3);
String udate = ret.getString(4);
System.out.println(uid + " " + ufname + " " + ulname + " " + udate );
}//显示数据
ret.close();
db1.close();//关闭连接
} catch (SQLException e) {
e.printStackTrace();
}
}
} 4、至此就完全了java+mysql服务器的搭建。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯