java+mysql搭建服务器的步骤如下:
1、在工程项目中导入mysql-connection-java-5016-binjar这个jar包;
2、编写一个数据库管理类(MyDBHelper)用来配置数据库的属性:
import javasqlConnection;
import javasqlDriverManager;
import javasqlPreparedStatement;
import javasqlSQLException;
public class MyDBHelper {
public static final String url = "jdbc:mysql://127001/student";
public static final String name = "commysqljdbcDriver";
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 {
ClassforName(name);//指定连接类型
conn = DriverManagergetConnection(url, user, password);//获取连接
pst = connprepareStatement(sql);//准备执行语句
} catch (Exception e) {
eprintStackTrace();
}
}
public void close() {
try {
thisconnclose();
thispstclose();
} catch (SQLException e) {
eprintStackTrace();
}
}
}
3、编写一个测试类(Test)来测试相关数据库 *** 作:
package comhudemo;import javasqlResultSet;
import javasqlSQLException;
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 = db1pstexecuteQuery();//执行语句,得到结果集
while (retnext()) {
String uid = retgetString(1);
String ufname = retgetString(2);
String ulname = retgetString(3);
String udate = retgetString(4);
Systemoutprintln(uid + "\t" + ufname + "\t" + ulname + "\t" + udate );
}//显示数据
retclose();
db1close();//关闭连接
} catch (SQLException e) {
eprintStackTrace();
}
}
}
4、至此就完全了java+mysql服务器的搭建。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)