2
回答
java连接数据库SQL操作
注册华为云得mate10,2.9折抢先购!>>>   
这个代码我是根据书上
一个连接数据库然后读取一个表 然后根据SQL语句打印出来某内容
改的 不知道什么地方错了
我也是东拼西凑的弄的 
运行结果是:
conneciton DBS sueecssful!
import map failed!
import map failed!
import map failed!
import map failed!
import map failed!
import map failed!
import map failed!
import map failed!
import map failed!

代码如下:
package com.linka.app;

import java.sql.*;


public class ConDBS {
private static boolean commit() {
return false;
}
private static void exec(String sql1) {
}
public static void main(String[] args){
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc:odbc:Northwind";
String username = "";
String password = "";
connectionDBS(driver,url,username, password);
}
public static void connectionDBS(String driver,
String url,
String username,
String password){
try{
Class.forName(driver);
Connection connection = DriverManager.getConnection(url);
System.out.println("conneciton DBS sueecssful!");
Statement statement = connection.createStatement();
String query = "SELECT firstname,lastname FROM employees";
ResultSet resultSet = statement.executeQuery(query);
while(resultSet.next()){
String sql1 = "CREATE TABLE CtypeONU";
String sql2 = "INSERT INTO CtypeONU VALUES (*) FROM 'query'";
exec(sql1);
exec(sql2);
boolean ret = commit();
if(ret) {
System.out.println("import map successful!");
} else {
System.out.println("import map failed!");
}
}
resultSet.close();
}
catch(ClassNotFoundException cnfe){
System.err.println("Error loading driver:" + cnfe);
}
catch(SQLException sqle){
System.err.println("Error with connection:" + sqle);
}
}
}



举报
钟跃民
发帖于6年前 2回/423阅
顶部